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

Does Perl have an include statement, or something similar to C's "#include"? I just took over support of a system that utilizes Perl scripts to create files from an Oracle db, and I'd like to know if I can use an include statment or similar to specify some information in a separate file. Thanks for any help!

Replies are listed 'Best First'.
Re: Include statement in Perl?
by dws (Chancellor) on Dec 11, 2001 at 02:19 UTC
    Does Perl have an include statement, or something similar to C's "#include"?

    The statement you're looking for is require. At a command prompt, typing perldoc -f require will get you more info. Also check out the use statement, which refines require in interesting and useful ways.

      Thanks, I'll try those.
Re: Include statement in Perl?
by grinder (Bishop) on Dec 11, 2001 at 02:46 UTC
    Apart from use and require it as also possible to do a file. On the file include spectrum, do would be the rawest method, and use would be the most refined.

    It should be noted in passing that #include as a concept doesn't really pan out in Perl. Yes, you can embed perl code in external files, but then you must ensure that the code is run a perfectly trusted environment, otherwise Really Bad Shi^WThings can happen. That is, even if a user cannot modify your code, if they can edit a config file that is Perl code waiting to be eval'ed, then you are wide open in terms of security vulnerabilities.

    A better approach is to use configuration files to store data, and parse it all. There are a number of modules that make this simple to do. For instance, if you are familiar with Win32-style .ini files, there is a module to do that, and a few more besides.

    In any event, you should also read perlsec and tainting, to be make sure that people don't try and make your program do naughty things, like rm -rf /.

    --
    g r i n d e r
    just another bofh

    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u';
Re: Include statement in Perl?
by chip (Curate) on Dec 11, 2001 at 07:01 UTC
    I should think a source filter would allow a literal include (along with lots of other mischief). But as my fellow monks have pointed out, just because you can do an inclusion doesn't mean you should.

        -- Chip Salzenberg, Free-Floating Agent of Chaos

Re: Include statement in Perl?
by herveus (Prior) on Dec 12, 2001 at 20:45 UTC
    Howdy!

    perldoc perlrun shows

    -P

    causes your script to be run through the C preprocessor before compilation by Perl. (Because both comments and cpp directives begin with the # character, you should avoid starting comments with any words recognized by the C preprocessor such as ``if'', ``else'', or ``define''.)

    This will let you use '#include'...but do be mindful of the security issues others have already raised...

    yours,
    Michael

Re: Include statement in Perl?
by atcroft (Abbot) on Dec 11, 2001 at 14:38 UTC

    My copy is at work, but isn't there also an example of using an eval statement in Perl CookBook if you're actually including code? (Maybe Programming Perl or one of the other O'Reilly Perl books-I'm too tired to remember as I type this.) Anyone?