Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Using subroutines in SSI

Hi,

I was looking for a method with which I can use user defined subroutines in SSI version 8.4.8. While looking for help, I found the below thread:

https://www.sawtoothsoftware.com/forum/13961/perl-modules-in-ssi-web-for-code-reusability

I tried to follow the instructions but it did not work for me. What I did to test this was defines a file
subs.pl
inside my
graphics
folder with the following code:

sub area {
    # passing argument
    my $side = $_[0];
    return ($side * $side);
}

sub qname {
    return QUESTIONNAME();
}


In my SSI I created a text element and put the following code in the header1 and header2:

header1:
[% Begin Unverified Perl
require '../graphics/subs.pl';
return area(4);
End Unverified %]


header2:
[% Begin Unverified Perl
require '../graphics/subs.pl';
return qname();
End Unverified %]


When I uploaded and tried to run this link, it raises the following error in the error log:
Server Error: Can't locate ../graphics/subs.pl in @INC (@INC contains: /var/www/cgi-bin/ssiweb848 /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .)


What might be the reason and fix for this?
asked Jul 31, 2019 by prashant (180 points)
First thing that sticks out is that Perl modules must return true in order to load properly.  Please append this line to the end of your .pl file:

 1; 
Hi Prashant,

I could see that you have used "QUESTIONNAME();" in your "subs.pl" file, which can't be accessed directly since that function name is not present in this "subs.pl" file. So you should use something like below

sub qname {
    return authlib8_4_8::QUESTIONNAME();
}

Your solution to the original question

Please only use this to answer the original question. Otherwise please use comments.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:

To avoid this verification in future, please log in or register.
...