Re: Include perl sources with require
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 5 Apr 2006 10:12:39 -0700
fritz-bayer@xxxxxx wrote:
I'm trying to include some source code into my perl program using
require like this:
require "myFunctions.pl";
All perl sources of mine are in one directory. I inserted the require
line at the end of the main program.
That's the wrong approach.
The file "myFunctions.pl" contains
functions, which I use in several perl programs.
When running the main program I get an error saying that the fuction
xzy() can not me called. Looks like this:
Undefined subroutine &main::testFunction called at
/home/fritz/perl/main.pl
Of course you do. The functions don't exist until after you tried
calling them. require() happens at run-time, not at compile time.
If I put the require line at the very top of "main.pl", then I get the
following error message:
myFunctions.pl did not return a true value at /home/fritz/perl/main.pl
line 26.
What am I doing wrong?
Why ask us, when Perl can tell you itself?
perldoc perldiag
%s did not return a true value
(F) A required (or used) file must return a true value
to indicate that it compiled correctly and ran its
initialization code correctly. It's traditional to end
such a file with a "1;", though any true value would do.
See the require entry in the perlfunc manpage.
Seems that my functions in "myFunctions.pl" are
not in the scope of the "main.pl" file !?
Correct.
The "normal" way of including files is with the 'use' function. 'use'
will 'require' your file, plus call import on the given package name,
in case your module exports anything. Better still, 'use' happens at
compile time.
perldoc -f require
perldoc -f use
Paul Lalli
.
- References:
- Include perl sources with require
- From: fritz-bayer@xxxxxx
- Include perl sources with require
- Prev by Date: Re: Perl and Excel
- Next by Date: Re: Perl help
- Previous by thread: Include perl sources with require
- Next by thread: Re: Include perl sources with require
- Index(es):
Relevant Pages
|