For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > September 2005 > including or calling another perl script that has common code/functions









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author including or calling another perl script that has common code/functions
Hendry Taylor

2005-09-05, 7:09 pm

I would like to have a Perl script that has some common functions etc
and then call it from other Perl scripts. This common Perl script may
return an array or a number of values depending on the function.

Currently I am using "require" and it works, but I am not sure if that
is the most efficient way of doing it. One problem I have found with the
require way of doing it is that it seems to loose my first value
returned if i return numerous values at the same time.
Jim Gibson

2005-09-05, 7:09 pm

In article <dfi7fi$md3$1@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com>, Hendry
Taylor <hendry.taylor@btinternet.com> wrote:

> I would like to have a Perl script that has some common functions etc
> and then call it from other Perl scripts. This common Perl script may
> return an array or a number of values depending on the function.


Your "script" should not return anything but a true value if you are
using "require". The functions in the script, however, may return any
number of arguments.

>
> Currently I am using "require" and it works, but I am not sure if that
> is the most efficient way of doing it. One problem I have found with the
> require way of doing it is that it seems to loose my first value
> returned if i return numerous values at the same time.


You should be calling "require" only once on your script file. You
should not be saving the return value from the require, as the return
value is an indication to the Perl compiler whether or not the require
was successful. This is best done by putting a bare "1;" at the end of
the script.

Having executed a "require script.file" statement, you may then call
any subroutines defined by script.file as if they had been defined by
your program. Calling "require script.file" a second time will do
nothing, as the runtime will ignore the call.

See "perldoc -f require" for details.

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com