Have an idea?

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

Perl Modules in SSI Web for code reusability

Hi Team,

We are working on creating a user defined perl module with frequently used perl codes so that we can reuse the code whenever required by loading module 'use' command.

We were able to define a sub routine in perl and it is working fine in that particular question. Now we are trying to place it as a module so that we can call the module and use sub routine directly instead of again copy pasting the sub routine.

we are looking forward for something like a .js file in graphics path used for placing all custom scripts and can be loaded by using <script> tag in header / footer. Is there a way to achieve this for perl codes aswell in graphics folder?

We have created a module but we are not able to retrieve the module or change the working directory.  
Below server error is shown while we are trying to load the user defined module.
Server Error: Can't locate lib.pm in @INC (you may need to install the lib module) (@INC contains: C:/.../SSI Web/LocalWeb/htdocs/8_4_8/Template/cgi-bin C:\Users\admin\AppData\Roaming\Sawtooth Software, Inc\SSI Web\LocalWeb\htdocs\8_4_8\Template\cgi-bin) at (eval 86) line 2, <GEN2> line 18. BEGIN failed--compilation aborted at (eval 86) line 2, <GEN2> line 18.

SSIWebVersion= 8_4_8 : Script: use lib qw(GRAPHICSPATH()); use ListRand; return GroupRand(1,2); params= Password=>test, SampleConst=>4, Username=>test,

Is it possible to define and load perl modules from the graphics folder of the project?
 How can we change the Cwd (Current working directory) in the perl in SSI Web 8.4.8?

Thank You!
asked Jun 6, 2017 by Bharath Bronze (695 points)
retagged Jun 6, 2017 by Walter Williams

1 Answer

+4 votes
Best answer
This code loads a Perl file from the graphics folder and runs one of its subroutines:

[% Begin Unverified Perl
require '../graphics/FILE.pl';
return SUBROUTINE();
End Unverified %]

Note that the survey needs to be uploaded in order for this code to run.
answered Jun 6, 2017 by Zachary Platinum Sawtooth Software, Inc. (145,900 points)
selected Jun 8, 2017 by Bharath
Thanks Zachary. This is working fine and we are able to execute Perl from graphics path.
In the custom Perl file, if we want to get the data of a question (known dynamically based on code in the external Perl file) in the SSI, I suppose we need to connect to database and query the value.

Is there a way to call the GETVALUE() or VALUE() SSI functions in external .pl file.
I believe those functions should already work.  I set my external file to this:

sub mytest {
    return GETVALUE('Q1');


When I ran my previous code to call this subroutine, it printed out my response for Q1.
Thanks you so much Zachary, it is absolutely working fine.

Whenever we required to call a subroutine defined in our package, we are including this statement
  require '../graphics/FILE.pl',   
in that question. If we require it in list, we are calling in the constructed list. Instead of this, can we include the statement at one place in the SSI tool, and can use through out the survey in all the questions.

We have tried by placing this statement in global header,  but it didn't worked out.
Also we have tried using,
 use lib '../graphics/FILE.pl'; 
 push ( @INC,"../graphics/FILE.pl"); 
But could see the server error "Can't locate lib.pm in @INC ...".

Thanks in advance!
Thanks Zachary. We have tried using the above approach and it is working fine.

But when we use these functions in our own defined Perl package, we are shown with an error "undefined subroutine in package X" once we run the code.

Can you please let us know in which Perl file these predefined Perl functions are located. So that we can call these functions in our package by using below syntax.

Our code in external .pl file will be as:

Package X;
sub mytest {
    return PackageY::GETVALUE('Q1');

Thanks in advance.
@Harika: I'm not sure there is any one place you can load your Perl library and have it available everywhere.  I did some testing and found that code like this works when I put it into a text question:

[% Begin Unverified Perl
require '../graphics/FILE.pl';
return '';
End Unverified %]

[% Begin Unverified Perl
return SUBROUTINE();
End Unverified %]

But that code does not work if I move the first half into the survey's header.

At this time, the only way I can think of to load the library everywhere would be to modify the survey's .pl files, but adding the require statement to your unverified Perl codes is probably the easier route.
@Karthik: Is there more code in your external .pl file?  Where is PackageY defined?  Also, can you share the survey code you are using to call mytest?

I believe GETVALUE and all the other unverified Perl functions are in authlibX_X_X.pl, where "X_X_X" is the version of Lighthouse you are running.
Hi Zachary,

We haven't shared you the entire code which is in our external .pl file, we have just shared you the example for your understanding.

However, we have checked the authlibX_X_X.pl file and could find all the global functions in that file and we are now able to achieve the output we require.

Thank you for all your answers over these questions.
Thanks Zacharay for looking into this and providing your thoughts.

We do think that, including the require statement wherever needed will be the good way as of now.