As mentioned above, you can use do "filename" to do what include() does for PHP. However, though it may make some kind of sense in PHP, it is usually not what you want when you're writing programs instead of combining template fragments.

Consider that do() will compile and execute all the code in the included file each time you run it call do(). This is not how you normally work when you're structuring your program accross multiple files. Generally, you want to structure your program so that functions and classes are grouped logically. You normally don't have your "main function" running over multiple files.

But functions and classes only need to be defined once. Now consider that you might want to use the functions defined in file "stuff.pl" from the the files "foo.pl" and "bar.pl". The logical way to state that dependency is to "include" the "stuff.pl" in both "foo.pl" and "bar.pl". But if "foo.pl" and "bar.pl" are both needed for your main program, you're including "stuff.pl" twice.

That's where perl's require("filename") function comes in to play. require() will only load and execute the speficied file the first time you call it. Subsequent calls to require() with the same file-path will do nothing, so it's safe to require the same dependencies in multiple files. For error checking, require() will die if the included file does not end with (returns) a true value.

Then there's the use Module::Name directive that works like require() but use() will load and execute the file as soon as possible (at compile time), it doesn't take a file path as an argument, but a package name or module that gets converted to a file path, and then tries to call the module's import() method if it exists. **

Just like require(), all files will only be loaded once, but for each call to use() the package's import() method will be called. That way a module can take additional arguments at load time and possibly modify the caller. Modules using Exporter for instance can introduce variables and function into the calling package without knowing up-front which packages will be using them.

** update: you can also specify a Module::Name as an argument to require() but only use() will load at compile time and call the import() method. The biggest advantage of using a module name is that you don't have to specify the exact file path of the module - perl will search for the module in a predefined set of directories, which you can add to with the use lib '/path/' statement if you need to.


In reply to Re: calling a perl file from a perl script by Joost
in thread calling a perl file from a perl script by hodashirzad

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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, details, 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, summary, 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.