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

I'm having trouble exporting a subroutine from an external .pm file.
In the file that calls the subroutine I have the code
#!/usr/bin/perl -wT use strict; use CGI qw(:standard); use lib "/home/public_html/cgi-bin"; use DATA99 qw(WasonE);
From what I understand using that code it should know to look in /home/public_html/cgi-bin for the DATA99.pm file and then call the WasonE subroutine.
In the DATA99.pm file I have
package DATA99; require Exporter; our @ISA = ("Exporter"); our @EXPORT_OK = qw(WasonE); sub WasonE { variables... } 1;
From what I understand from this it packages the DATA99 file and Exports it, then Exports the Subroutine WasonE if a script asks for it (our @EXPORT_OK = qw(WasonE);).
I can get the page to pull up with out one of those fun 500 error messages, but when I look at my error log it says I have a bunch of undefined variables.
If you couldn't tell I'm new to the ways of Perl.

Replies are listed 'Best First'.
Re: external subroutine
by saskaqueer (Friar) on May 20, 2004 at 04:46 UTC

    Everything you have said is correct, but for the phrase "using that code it should... call the WasonE subroutine". The code you have posted doesn't automatically call the WasonE subroutine, it is just imported into your namespace. You need to add a manual call to the subroutine:

    #!/usr/bin/perl -wT use strict; use CGI qw(:standard); use lib "/home/public_html/cgi-bin"; use DATA99 qw(WasonE); WasonE();

    As for the errors being genereated in your error log, it has nothing to do with the code segments you've posted here. The warnings would be coming from within the WasonE() subroutine and are being added to your error log because you've globally enabled warnings (via the '-w' switch on the shebang line). Try adding the WasonE(); line and if you've programmed things correctly/logically, it should work fine.

      It made a lot of sense, to call the subroutine, almost too much. But unfortunatly it still does not work.
      I now have this all over my error log:
      Use of uninitialized value in string ne at WasonE.cgi line 191. Use of uninitialized value in string ne at WasonE.cgi line 187.
      The ne that it is refering to is:
      if ($WasonE{HSname} ne "") { print "<th>" . $WasonE{HSname} . "</th>\n"; }
      The whole ne is to say if the value is not black print the value. I know it worked before because before I tried to make it an external file it worked fine.
      Further advice?

        Ah k, in cases where you want to print out a value only if it is 'blank', you want to test for a defined value rather than a blank one:

        print "<th>$WasonE{HSName}</th>\n" if ( defined( $WasonE{'HSName'} ) );

        This will get rid of the warnings you are seeing in your log file, though this will not fix any actual errors. When you run the script in the browser, does it still work as expected, even though you have a lot of warnings in the error log?

        I think you'll need to show a little more code for this to make sense. The fact that the perl interpreter seems to think that "ne" is a string that is supposed to be initialized with a value leads me to think that you have a mis-matched bracket somewhere, or some similar sort of typo that is throwing off the syntax in your cgi script. Or maybe it's a mis-matched quote operator somewhere, or a misplaced "#".

        If the editor you use for writing perl scripts supports jumping to a matching bracket, use that facility on the open and close parens, open and close curlies, and any nearby square brackets on and near the line that triggered the error. Syntax highlighting, which renders quoted strings and comments in a different colors than the rest of the code, can also be a big help.

        Nevermind -- sorry to have misread the warning message.

Re: external subroutine
by Scarborough (Hermit) on May 20, 2004 at 10:09 UTC
    This wont help your cure the problems but it will make them easy to find if you use strict in your modules as recommended in Simple Module Tutorial It helped me a lot when I tried to write modules for the first time.
      got it ;)