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

One of my collegues from the C world would like to know if it is possible to split a single PERL script across multiple files without using packages and the like. He says it would be something similar to using "include" files in C. Many thanks for your help.

Replies are listed 'Best First'.
Re: Multiple Files, One Script Possible?
by Arif (Acolyte) on Feb 28, 2008 at 18:22 UTC
    Something like the following will work:
    #!/usr/bin/perl use 5; use warnings; use strict; require ""; require ""; require ""; require "";
    but I wouldn't use 'include' files in C like this either.
      Without packgages, do is more appropriate than require. Either way, it's just bad code.
      TMTOWTDI, and this works but I wouldn't do it either... ;)
      $ cat | perl
      IMHO, refactoring a large, one file only program by using modules seems the best course of action in the long term.
Re: Multiple Files, One Script Possible?
by NetWallah (Canon) on Feb 28, 2008 at 18:25 UTC
    Or, you could just do it ! (With apologies to Nike).

         "As you get older three things happen. The first is your memory goes, and I can't remember the other two... " - Sir Norman Wisdom

Re: Multiple Files, One Script Possible?
by moklevat (Priest) on Feb 28, 2008 at 18:24 UTC
    I think you are looking for require.
Re: Multiple Files, One Script Possible?
by papidave (Pilgrim) on Feb 28, 2008 at 19:58 UTC
    I think that the confusion here is that inclusion of a file is generally tied closely to the specification of a separate package namespace. This is a good idea (to avoid collisions between variable and subroutine names), but isn't really required. Consider program

    You get the expected results -- that subroutines foo1 and foo2 are called from their corresponding modules.

    Unlike (for example) Pascal, the Perl programmer is not burdened with the arbitrary preferences of the person who specified the language. TMTOWTDI. This is not merely a slogan, it's a language reality, and you have hereby been provided with more than enough rope to hang yourself. Use it in good health.

Re: Multiple Files, One Script Possible?
by chromatic (Archbishop) on Feb 28, 2008 at 22:02 UTC

    C's include files generally don't bring in code. (At least, well-designed include files don't.) They just declare symbols referred to in other compilation units.

    Because there's no separation between compilation and linking in Perl, this sort of thing is unnecessary.

    Besides that, PERL is a web scripting language, so it's not even compiled. It's completely interpreted, and if you can include other files, it's probably a horrible hack and a security flaw.

    If you can avoid PERL and use Perl instead, I really do recommend that you use packages and modules to structure your programs. Work with the language, not against it. The tools, documentation, and support work so much better that way, not to mention that the language really encourages that kind of modularity.