singlespeed has asked for the wisdom of the Perl Monks concerning the following question:

I have a project that I've done in perl script. It's a bunch of simple scripts that parse up csv files in different formats and put selected data from them into a common table. I've expanded these individual scripts and have at least one section that is common to all. For the time being, it's better to keep the scripts separate.

What I'd like to do however is take that common section out of all the scripts, put it in it's own script so that it can just be called by the others. This section however must return an array of filenames to the "parent" script. Is this possible in perl?

I know in PHP you just do an include(scriptname) but I don't know how to accomplish this in perl.

thanks,

jeff

  • Comment on Using php like includes in perl script?

Replies are listed 'Best First'.
Re: Using php like includes in perl script?
by davidrw (Prior) on Feb 22, 2006 at 16:12 UTC
    Take a look at the do function .. You can also create a module for your common code .. take a look at the "Writing, Installing, and Using Perl Modules" section of the Tutorials.. especially Including files
      thanks! I'll check that out.
Re: Using php like includes in perl script?
by davorg (Chancellor) on Feb 22, 2006 at 16:38 UTC

    You can use "do" as davidrw has already said. But a more scalable approach might be to create a real module and to use that in all of your other programs. A simple module that just contains a few functions would look like this.

    package My::Module::Name; require Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(some_function another_function); sub some_function { print "This is some function\n"; } sub another_function { print "This is another function\"; } 1;

    So it's really not as complex as many people seem to think.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg