Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Including files

by Anonymous Monk
on Sep 24, 2004 at 22:05 UTC ( #393690=note: print w/ replies, xml ) Need Help??


in reply to Including files

How about

require "some/file.pl" ;
?!!!

This will insert the code in the current package, the only required thing is that file.pl need to end with 1; (return a true value).


Comment on Re: Including files
Download Code
Re^2: Including files
by Juerd (Abbot) on Sep 24, 2004 at 23:44 UTC

    Sigh. Please try to READ the article you reply to next time.

      Yeah, well, I can't speak for the person you're sighing about but I did read the article. It says "Only in the simplest of situations you can get away with abusing require as if it were some kind of include(), but it's easy to get bitten by how it works: it only loads any given file once."

      I don't get it. That's *exactly* how I want includes to work. In fact, in C I have to add macro definitions and ifs to get include files to include only once. So now tell me again why I'm not supposed to just use require to read in a library of functions? I guess I'm just simple. So what am I missing other than modules are a cooler way to do it?

        Sorry, I think you’re reading Juerd’s tutorial back-to-front.

        So now tell me again why I'm not supposed to just use require to read in a library of functions?

        Well, it’s usually better to say:

        use MyModule LIST;

        because that’s a convenient shorthand for:

        BEGIN { require Module; Module->import( LIST ); }

        which is likely to be what you want (see use.) But whether it’s use or require, that’s exactly what you are supposed to use to “read in a library of functions”. Juerd isn’t telling you not to use require, he’s telling you that neither require nor do will give you the equivalent of C’s #include.

        In fact, in C I have to add macro definitions and ifs to get include files to include only once.

        Exactly! But Juerd’s point is that some programmers want to use #include to include the same code in multiple places, and that won’t work with require. It will work with do, except that “code evaluated with do FILENAME cannot see lexicals in the enclosing scope” — do. So if you have a file “foo.pl”:

        $foo = 42; print "In foo.pl: \$foo = $foo\n"

        which you then try to “include” in another file using do:

        use strict; use warnings; my $foo = 17; print "Before do: \$foo = $foo\n"; do 'foo.pl'; print "After do: \$foo = $foo\n";

        the result is probably not what you wanted:

        13:54 >perl 990_SoPW.pl Before do: $foo = 17 In foo.pl: $foo = 42 After do: $foo = 17 13:54 >

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        Yeah, well, I can't speak for the person you're sighing about but I did read the article...I don't get it.

        Juerd wrote the article in 2004, and if you don't get it, then you didn't really read it

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://393690]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (11)
As of 2014-11-24 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (141 votes), past polls