Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

<Help> How to use routines from another perl script

0 views
Skip to first unread message

baiya...@gmail.com

unread,
Oct 6, 2008, 7:12:40 AM10/6/08
to baiya...@126.com
Hi, All,

I am just a novice to perl, I want to reuse a routine wrote in one
perl script to all other perl scripts, just like c, c++ do, but I
don't know how to "include" another perl script into current perl
script to utilize the routines. would anyone give some tips on it?

Thanks so much.

Baiyan

Jürgen Exner

unread,
Oct 6, 2008, 9:25:00 AM10/6/08
to

Typically you would create a module and import its functions into your
main program using 'use'.

There is also 'do' which is kind of the poor man's 'use'.

jue

darkon

unread,
Oct 6, 2008, 11:14:41 AM10/6/08
to
Jürgen Exner <jurg...@hotmail.com> wrote:

And I suppose we might as well add the following; a novice might
not (yet) be accustomed to searching the voluminous FAQ files.


perldoc -q module


Found in C:\Perl\lib\pod\perlfaq7.pod

How do I create a module?
(contributed by brian d foy)

perlmod, perlmodlib, perlmodstyle explain modules in all the gory
details. perlnewmod gives a a brief overview of the process along
with a couple of suggestions about style.

Petr Vileta (fidokomik)

unread,
Oct 6, 2008, 7:37:40 PM10/6/08
to

A simplest way is to write routines into separate files

--- example (/var/myroutines/ex1.pl) ---
1;
sub myfunc1 {
my ($param1,$param2) = @_;
my $to_return='';
# some code here
return $to_return;
}
--- example ---

and "include" it in main program like this:

#!/usr/bin/perl
use strict;
require "/var/myroutines/ex1.pl;
my $val = myfunc1(1, 'abc');

--
Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail.
Send me your mail from another non-spammer site please.)
Please reply to <petr AT practisoft DOT cz>

Tim Greer

unread,
Oct 6, 2008, 11:12:14 PM10/6/08
to
Petr Vileta (fidokomik) wrote:

> baiya...@gmail.com wrote:
>> Hi, All,
>>
>> I am just a novice to perl, I want to reuse a routine wrote in one
>> perl script to all other perl scripts, just like c, c++ do, but I
>> don't know how to "include" another perl script into current perl
>> script to utilize the routines. would anyone give some tips on it?
>>
>> Thanks so much.
>>
>> Baiyan
>
> A simplest way is to write routines into separate files
>
> --- example (/var/myroutines/ex1.pl) ---
> 1;

^ Down there, maybe --> ?

> sub myfunc1 {
> my ($param1,$param2) = @_;
> my $to_return='';
> # some code here
> return $to_return;
> }

1;

> --- example ---
>
> and "include" it in main program like this:
>
> #!/usr/bin/perl

use warnings;

> use strict;
> require "/var/myroutines/ex1.pl;

|| die, or die, or use eval.


--
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!

Petr Vileta (fidokomik)

unread,
Oct 7, 2008, 12:12:04 PM10/7/08
to
Tim Greer wrote:

> Petr Vileta (fidokomik) wrote:
>
>> --- example (/var/myroutines/ex1.pl) ---
>> 1;
>
> ^ Down there, maybe --> ?
>

Yes, of course ;-) But for some reason (I forgot why) I tend to write it to top
of file.

>> sub myfunc1 {
>> my ($param1,$param2) = @_;
>> my $to_return='';
>> # some code here
>> return $to_return;
>> }
>
> 1;
>
>> --- example ---
>>

> use warnings;
>

use strict;
no warnings;

:-)

>> require "/var/myroutines/ex1.pl;
>
>>> die, or die, or use eval.

Of course, "die" should be there, but this is a fast-written example only.

Tim Greer

unread,
Oct 7, 2008, 4:16:48 PM10/7/08
to
Petr Vileta (fidokomik) wrote:

> Tim Greer wrote:
>> Petr Vileta (fidokomik) wrote:
>>
>>> --- example (/var/myroutines/ex1.pl) ---
>>> 1;
>>
>> ^ Down there, maybe --> ?
>>
>
> Yes, of course ;-) But for some reason (I forgot why) I tend to write
> it to top of file.
>
>>> sub myfunc1 {
>>> my ($param1,$param2) = @_;
>>> my $to_return='';
>>> # some code here
>>> return $to_return;
>>> }
>>
>> 1;
>>
>>> --- example ---
>>>
>
>> use warnings;
>>
>
> use strict;
> no warnings;
>
> :-)
>
>>> require "/var/myroutines/ex1.pl;
>>
>>>> die, or die, or use eval.
>
> Of course, "die" should be there, but this is a fast-written example
> only.
>

All humor aside, it's a good idea to give examples that are correct and,
if you can, add some common error checks. I wrote a pretty lengthly
blog article about this and why it's a good idea (it basically prevents
follow up questions when your example doesn't work for the OP).

Hans Mulder

unread,
Oct 7, 2008, 6:45:57 PM10/7/08
to
Tim Greer wrote:
> Petr Vileta (fidokomik) wrote:

>> #!/usr/bin/perl
>
> use warnings;
>
>> use strict;
>> require "/var/myroutines/ex1.pl;
>
> || die, or die, or use eval.

Why use "die? "Require" already "die"s when something is wrong.

Were you thinking of "do", perhaps?

-- HansM

Tim Greer

unread,
Oct 7, 2008, 7:01:57 PM10/7/08
to
Hans Mulder wrote:

I was actually making a point about the error (typo) he made in his
example... as in "what did you want it to do here?" in an attempt at
humor.

Notice:

require "/var/myroutines/ex1.pl;

is missing the closing ". Sorry, that probably came out more sarcastic
than humorous (it was meant in a lighthearted way), but I was serious
about checking the calls.

Petr Vileta (fidokomik)

unread,
Oct 8, 2008, 8:33:34 PM10/8/08
to
Tim Greer wrote:
> Notice:
>
> require "/var/myroutines/ex1.pl;
>
Oh so ;-( I must change my glasses I think ;-)

Peter J. Holzer

unread,
Oct 11, 2008, 4:06:36 AM10/11/08
to
On 2008-10-07 23:01, Tim Greer <t...@burlyhost.com> wrote:
> Hans Mulder wrote:
>> Tim Greer wrote:
>>> Petr Vileta (fidokomik) wrote:
>>>> require "/var/myroutines/ex1.pl;
>>>
>>> || die, or die, or use eval.
>>
>> Why use "die? "Require" already "die"s when something is wrong.
>>
>> Were you thinking of "do", perhaps?
>>
>
> I was actually making a point about the error (typo) he made in his
> example... as in "what did you want it to do here?" in an attempt at
> humor.
>
> Notice:
>
> require "/var/myroutines/ex1.pl;
>
> is missing the closing ".

That, however, would be caught at compile time, so the presence or
absence of "die" wouldn't make a difference.

hp

Tim Greer

unread,
Oct 14, 2008, 2:26:36 PM10/14/08
to
Peter J. Holzer wrote:

Yes it would. And indeed, die wouldn't make a difference. This is why
I mentioned the attempt at humor. Pardon that it just caused
confusion.

0 new messages