Re: Include perl sources with require



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

.



Relevant Pages

  • Re: Net::DNS
    ... function because you still use $rr within the foreach, ... > So I can find the all these object methods in the perl docs listed below? ... > the return value of 'grep'. ... > perldoc perlboot ...
    (perl.beginners)
  • Re: Posting Guidelines changes (was Re: variable interpolation of refs to anonymous subroutines)
    ... Experts at Perl or experts at posting to newsgroups? ... use warnings; # see perldoc warnings ...
    (comp.lang.perl.misc)
  • Re: Multitasking
    ... Just a comment for the archives, and to any other beginner to the Perl ... When you look at the perldoc for select, perldoc -f select, you ... select FILEHANDLE ... I just meant avoid using the "Low-level socket functions" ...
    (perl.beginners)
  • Re: Teach me how to fish, regexp
    ... Thanks for your response on this thread: ... On the other hand, maybe "older" perl is ... > everything up in perldoc and man pages. ... > pattern so that a quantifier can apply to it. ...
    (comp.lang.perl.misc)
  • Re: Parse and Compare Text Files
    ... > I'm new to Perl and have what I hope is a simple question: ... > log with the categorized domains list file and creates a new file that looks ... perldoc -f print ... open file for writing to store updated log to ...
    (perl.beginners)