Mailing List Archive

Undefined subroutine
Hi,

Below I have listet relevant code of two perl files I'm working on.
The problem is that "sub loadFileText" in files.pl, cannot be seen from the
file menus.pl, but "sub load_file" can.
For some reason, which I can't seem to find, the code generates the error
"Undefined subroutine".
Why?

files.pl:
----------
#!perl.exe

use fcntl; # File Control

sub load_file
{
my ($filename) = @_;
my @lines;

sysopen(FH, $filename, O_RDONLY) # Opens the file
or die "Cannot open $filename";
@lines = <FH>; # Reads the entire filecontent into an array of lines
close(FH); # Close the opened file

return @lines;
}

sub loadFileText
{
my $filename = $_[0];

my $text = "";
my @lines = load_file($filename);

for(my $i=0; $i<scalar(@lines); $i++)
{
$text .= $lines[$i];
}

return $text;
}

1;

menus.pl:
--------------
#!perl.exe

use XML::Parser::Lite; # XML Support
require "../../cgi-bin/soren/files.pl";

sub showMenu
{
# Parameters:
my $sSource = $_[0];
my $sID = $_[1];
$sLanguageID = $_[2];

my $xmlDoc = loadFileText($sSource); # <=== loadFileText is
undefined ??????
my $sMenu .= createMenu($xmlDoc, $sID);

print $sMenu;
}

1;

/Søren
Re: Undefined subroutine [ In reply to ]
Søren Tauber Lassen wrote:

> files.pl:

Rename it to files.pm, and put it in the same directory as your
global.asa. Say "use files" at the top of global.asa. Restart the web
server.

> sub loadFileText

Within files.pm, say "require Exporter;" and read the documentation on
the Exporter module to find out how to use it to export loadFileText
into the global namespace.

> menus.pl:

This should probably be another Perl module, not a *.pl file.

> require "../../cgi-bin/soren/files.pl";

If you need routines from files.pm here, you shouldn't need a require or
use statement here if menus.pm is loaded from another module that does
load in files.pm.

---------------------------------------------------------------------
To unsubscribe, e-mail: asp-unsubscribe@perl.apache.org
For additional commands, e-mail: asp-help@perl.apache.org