Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

calling perl subroutines in other files

by drock (Beadle)
on Sep 26, 2004 at 08:03 UTC ( #393896=perlquestion: print w/replies, xml ) Need Help??

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

Yo... perl-ers wazzzz up? I want toc call a subroutine from a seperate file while using a data file as input. Do I have create a package then use "use"? Theses are 2 seperate files. Can anyone offer some example code? thanks, derek

20040927 Edit by castaway: Changed title from 'remote subroutine call'

  • Comment on calling perl subroutines in other files

Replies are listed 'Best First'.
Re: calling perl subroutines in other files
by tachyon (Chancellor) on Sep 26, 2004 at 08:13 UTC

    You can do it with use or require. Making your common subroutine code into a library (aka module) is the accepted way. See Simple Module Tutorial for sample code that shows you how. It is really very easy.



      tachyon, this is great thank you! But I am still trying to understand this. Where do I want to set this up at, in my MAIN program where the file is created or in the subroutine file where the actual work is to be done? Here are some specific questions: how does the calling program ( call the remote files subrouting) know based off your use vars? Is there a mored detailed man page I can read and maybe I will answer my own questions! thanks derek

        The typical construction of a perl widget is to put most/all your subroutines in one or more modules. These modules are your 'subroutine files'. You can save them anywhere. In your script you "use lib '/my/modules/are/here'" to tell Perl where to look for your modueles and then use whichever of your modules you want. Typically the subroutines in one module all relate to one sort of thing. For example DBI is a module for database accesss, Digest::MD5 gives you MD5 hashing.....

        There is plenty of info in the perl docs. See perlman:perlmod perlman:perlmodlib perlman:perlmodinstall perlman:perlnewmod



Re: calling perl subroutines in other files
by Rhys (Pilgrim) on Sep 26, 2004 at 12:11 UTC

    While there are several ways that you can get around creating a module and doing use Mymod, it's not really a wise thing to do for several reasons. The main reasons are:

    1. If you have a look at many of the modules on CPAN, they do a lot of strange, interesting, and sometimes evil things... internally. But since they are properly-built modules, the calling code doesn't have to worry about this. Scoping your variables properly will achieve most of this as well, but making the module is better.
    2. do "" assumes you know the name and location of the file. use package works based on @INC, which you can change, and the package name, which you control. (Yes, the filename still comes into play, but it's a bit cleaner.) This means it works in Windows and Un*x, since you don't have to use system-specific paths/delimiters/conventions.
    3. It's kind of silly to circumvent use and require. They were designed to do what you want, and work well. C'mon! Everybody's doing it. You want to be cool, don't you? ;-)

    Seriously, though, when you consider that h2xs does most of the prep work for you, making a module out of a sub is pretty much cut, paste, change a few things in the header, save, done.


Re: calling perl subroutines in other files
by brycen (Monk) on Sep 26, 2004 at 10:15 UTC
    One is to evaluate the file containing the subroutine:
       eval `cat`;
    The subroutine will then be available to you. No "use" or "require" required.

        ....and evaluate why would you do that when you should be required to use the lovely module architecture ;-)

        As merlyn notes do is better than eval cat which is not portable.



Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2023-02-04 22:30 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (31 votes). Check out past polls.