Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

How can I write and call subroutines in separate files?

( #5880=categorized question: print w/ replies, xml ) Need Help??
Contributed by Anonymous Monk on Mar 22, 2000 at 11:00 UTC
Q&A  > subroutines


Description:

I want to separate some code into two files. Should I use modules? What's the simplest way of splitting code between files?

Answer: How can I write and call subroutines in separate files?
contributed by gridlock

What you can do is write your subroutines in seperate perl (.pl) files. Whenever you need to call them, all you need to do is include them with your code (use the require statement). Each file should end with a true expression, traditionally 1;.

Answer: How can I write and call subroutines in separate files?
contributed by chromatic

You can break up the code into logical units:

# file one use File2; setup(); main(); sub main { print "I am in the main subroutine.\n"; } # file 2 sub setup { print "Setting up.\n"; } 1; # use'd files have to return true!
Answer: How can I write and call subroutines in separate files?
contributed by cLive ;-)

If you're using strict, you need to declare the package name as well (and it's probably best to if your code's big enough to suggest splitting :)

# file one use File2; File2::setup(); main(); # or ::main() (this package) or main::main() very specific # "default" package name for main script (the one requiring other file +s) is 'main' # print var foo from package File2 print $File2::foo . "\n"; sub main { print "I am in the main subroutine.\n"; } # file 2 package File2; sub setup { print "Setting up.\n"; } $::foo = 'bar'; # or define explicitely $File2::foo = 'bar'; 1; # use'd files have to return true!
Answer: How can I write and call subroutines in separate files?
contributed by btrott

I think you're best off looking into putting your code into a module, whether that be basically a collection of utilities (where you may export certain routines) or an actual OO application. Which option you choose should depend on whether your program lends itself to an OO model--this won't necessarily be the case.

Take a look at use, package, perlmod, and the Exporter manpage. If you choose to go the object route, there's perlobj and some tutorials: perltoot and perlboot.

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (14)
    As of 2014-08-27 13:46 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (238 votes), past polls