Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How can I write and call subroutines in separate files?

by Anonymous Monk
on Mar 22, 2000 at 11:00 UTC ( [id://5880]=perlquestion: print w/replies, xml ) Need Help??

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

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

Replies are listed 'Best First'.
Re: How can I write and call subroutines in separate files?
by gridlock (Novice) on Feb 22, 2004 at 06:52 UTC
    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;.
      Hi, dont forget the 1; at the end of the require "file"; where file is:
      #!/usr/bin/perl sub one{ print "one"; } 1;
      otherwise the compiler wont be happy;-)
Re: How can I write and call subroutines in separate files?
by chromatic (Archbishop) on Apr 13, 2000 at 21:11 UTC
    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!
Re: How can I write and call subroutines in separate files?
by cLive ;-) (Prior) on Apr 02, 2001 at 07:31 UTC
    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!
Re: How can I write and call subroutines in separate files?
by btrott (Parson) on Mar 22, 2000 at 11:29 UTC
    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.

Re: How can I write and call subroutines in separate files?
by buttroast (Scribe) on Oct 07, 2004 at 16:45 UTC
    When I try this, I get "Can't locate test2.pm in @INC". My main file is called "test.pl", and the second file with the subroutine in it is called "test2.pl". Here is "test.pl":
    #!/usr/bin/perl -wT $|=1; print "Content-type: text/html\n\n"; use CGI::Carp('fatalsToBrowser'); use strict; use warnings; use CGI qw(:standard); use test2; $CGI::DISABLE_UPLOADS = 1; $CGI::POST_MAX = 51_200; $|=1; print header( "text/html" ), start_html(-title => "Hello World"); test2::hello(); print end_html();
    Here is "test2.pl":
    #!/usr/bin/perl -wT package test2; sub hello { print "HELLO!\n"; } 1;
    Is this something with the way my web server is setup that it doesn't allow multiple files or is it just a code problem? Thanks in advance.
      You are asking the question in the wrong place. Ask in the Seekers of Perl Wisdom section, not here. For your reference, the perl package should end with .pm extension, and then in your test.pl, add the directive use lib "./" before use test2;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://5880]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-03-29 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found