intranetman has asked for the wisdom of the Perl Monks concerning the following question:

I have a problem passing variables between files. The problem occurs not between the HTML and CGI file but between the CGI file and a PL file. Foremost, a user inputs a password into an HTML form. The CGI file then processes this data and calls a function from the PL file to encrypt the password. However, I am unsure of how to pass the data(/variable) from the CGI file to the PL file. I've tried using this to capture data:
sub encrypto($) { my ($password) = @_; ... }
However, this does not work. The PL file works by itself if I specify a password in the $password variable. My real question would be how to pass data between a CGI and PL file. Any hints would be helpful.

Replies are listed 'Best First'.
Re: Passing variables
by Zaxo (Archbishop) on Sep 09, 2004 at 21:51 UTC

    How are you loading the .pl file, and what do you expect it to do? The usual thing would be something like this,

    # in foo.cgi: my $password = 'thesecret'; require 'thefile.pl'; my $encoded = encrypto( $password );
    The phrasing 'pass data to the file' sounds like you are trying some other usage. You should look at scoping, particularly file scope, to see what data is shared with a loaded file.

    After Compline,
    Zaxo

      You were dead on with your assumption. My setup is as follows:
      #in foo.cgi my $password = 'thesecret'; require 'thefile.pl'; my $encode = encrypto($password); #in thefile.pl sub encrypto($) { # encryption algorithm ... return $Finished; }
      I then get an error message that says  thefile.pl did not return a true value at foo.cgi line 10. Line 10 is the  require 'thefile.pl' line.

        That error message would have simplified the answer. From perldoc -f require,

        The file must return true as the last statement to indicate successful execution of any initialization code, so it's customary to end such a file with "1;" unless you're sure it'll return true otherwise. But it's better just to put the "1;", in case you add more statements.

        After Compline,
        Zaxo