Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

How do I include a file?

( #73777=categorized question: print w/ replies, xml ) Need Help??
Contributed by eyal_kle on Apr 19, 2001 at 13:07 UTC
Q&A  > files


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

Answer: How do I include a file?
contributed by arhuman

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
Answer: How do I include a file?
contributed by satchboost

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.

Answer: How do I include a file?
contributed by I0

or to do it exactly like the C command #include you could use the -P switch

Answer: How do I include a file?
contributed by gregw

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)
Answer: How do I include a file?
contributed by ariels

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)).

Answer: How do I include a file?
contributed by redemption

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;

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (4)
    As of 2016-08-25 05:57 GMT
    Find Nodes?
      Voting Booth?
      The best thing I ever won in a lottery was:

      Results (353 votes). Check out past polls.