Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Storing Functions

by dcoleyoung (Initiate)
on Dec 12, 2005 at 19:51 UTC ( [id://516125]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, Now I picture you guys as the dudes in the Nepalese mountains after seeing "Batman Begins".

I want to store functions that I'm writing in a common file so different files can use them. My approach right now is to store them in a file with "1;" at the end so it can be interpreted. In the calling files I have the line: require './perl_func';

Is there a better way of doing what I'm trying to do (storing functions in a common place)?

Thanks, Dan

Replies are listed 'Best First'.
Re: Storing Functions
by xdg (Monsignor) on Dec 12, 2005 at 19:59 UTC

    You're on the right track already. Read Simple Module Tutorial for more.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Storing Functions
by JediWizard (Deacon) on Dec 12, 2005 at 19:55 UTC

    See use. You may also like to examine Exporter and package.


    They say that time changes things, but you actually have to change them yourself.

    —Andy Warhol

Re: Storing Functions
by GrandFather (Saint) on Dec 12, 2005 at 20:00 UTC

    That is the way of "storing" (I'd say "organising" or "grouping" functions). What you have created is a Perl Module (often the files have a .pm extension). Take a look at Simple Module Tutorial for a little more information.


    DWIM is Perl's answer to Gödel
Re: Storing Functions
by chas (Priest) on Dec 12, 2005 at 20:17 UTC
    For just loading your own functions, you can also use do 'perl_func';. There are some differences from require, although they aren't significant for what you seem to want. You might try perldoc -f require and perldoc -f do to compare.
Re: Storing Functions
by planetscape (Chancellor) on Dec 13, 2005 at 10:33 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-24 01:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found