Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

How do I include a file?

by eyal_kle (Initiate)
on Apr 19, 2001 at 13:07 UTC ( #73777=perlquestion: print w/replies, xml ) Need Help??

eyal_kle has asked for the wisdom of the Perl Monks concerning the following question: (files)

How do I include a file? Like the C command #include.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I include a file?
by arhuman (Vicar) on Apr 19, 2001 at 13:11 UTC
    Use :
    • use - Include at compile time (uses export mechianism)
    • require - Include at execution time (you'll have to handle exporting task)
    • do - Execute some code at execution time
Re: How do I include a file?
by satchboost (Scribe) on Apr 20, 2001 at 19:58 UTC
    This isn't an answer, per se, but more of a comment. In Perl, use and require are more than #include ever was in C. The mechanisms, while resulting in something similar, do different things. If you're writing in Perl, it would behoove you to actually go ahead and take a stab at understanding what's going on and why those decisions were made. How to find this out? Read Programming Perl. Then, read Advanced Perl Programming. Ask questions here. But, please ask questions on how to understand Perl, not on how to make Perl behave like C. Yes, it can, but I feel you're missing the point.
Re: How do I include a file?
by I0 (Priest) on Apr 20, 2001 at 10:45 UTC
    or to do it exactly like the C command #include you could use the -P switch
Re: How do I include a file?
by gregw (Beadle) on Apr 20, 2001 at 12:41 UTC
    A ex-C programmer who doesn't want to create EXPORT lists might like the following variant:
    • BEGIN { require "filename"; } - Include at compile time (you'll have to handle exporting task)
Re: How do I include a file?
by ariels (Curate) on May 22, 2002 at 12:55 UTC
    Notes on 2 of the above answers:
    • A Perl programmer who doesn't want to create <samp>EXPORT</samp> lists will use the simpler form
      use Module ();

      Also, note the subtle differences between require "Name" and require Name...

    • There's a difference between use and require (on the one hand) and do (on the other).

      <samp>do</samp> essentially reads and evals the file; like any <samp>eval</samp>, it returns the value calculated. This value may be logically true, or it may not be; it doesn't matter.

      <samp>require</samp> (and therefore <samp>use</samp> too) attempts to ensure some semantics are loaded. This might fail! So a required (or used) module is required (sorry) to return logical true if successful, or logical false if not. Thus the need to end those files with "1;" (or any other truth!). It's not needed if just <samp>do</samp>ing something (e.g. if loading a configuration file (unsafely)).

Re: How do I include a file?
by redemption (Sexton) on May 22, 2002 at 08:29 UTC
    also remember to add a 1; at the end of the included file so that the Perl compiler doesn't complain that it doesn't return a value.
    # included file # code here 1;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2021-04-20 05:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?