Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > <Help> How to use routines from another perl script

Reply
Thread Tools

<Help> How to use routines from another perl script

 
 
baiyanhuang@gmail.com
Guest
Posts: n/a
 
      10-06-2008
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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      10-06-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>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?


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
 
Reply With Quote
 
 
 
 
Petr Vileta \(fidokomik\)
Guest
Posts: n/a
 
      10-06-2008
(E-Mail Removed) 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;
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>

 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      10-07-2008
Petr Vileta (fidokomik) wrote:

> (E-Mail Removed) 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!
 
Reply With Quote
 
Petr Vileta \(fidokomik\)
Guest
Posts: n/a
 
      10-07-2008
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.

--
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>

 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      10-07-2008
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).
--
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!
 
Reply With Quote
 
Hans Mulder
Guest
Posts: n/a
 
      10-07-2008
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

 
Reply With Quote
 
Tim Greer
Guest
Posts: n/a
 
      10-07-2008
Hans Mulder wrote:

> 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?
>


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.
--
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!
 
Reply With Quote
 
Petr Vileta \(fidokomik\)
Guest
Posts: n/a
 
      10-09-2008
Tim Greer wrote:
> Notice:
>
> require "/var/myroutines/ex1.pl;
>

Oh so ;-( I must change my glasses I think
--
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>

 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      10-11-2008
On 2008-10-07 23:01, Tim Greer <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Problem in passing values to perl script using another perl script vikrant Perl Misc 4 08-10-2005 06:20 PM
CGI: Execute a perl script inside another perl script xdarcos@hotmail.com Perl Misc 20 01-18-2005 12:33 PM
Execute another perl script from my perl script Petterson Mikael Perl Misc 3 01-05-2005 01:31 PM
How to make Perl Script "POST" call from another Perl Script??? Wet Basement Perl 1 07-15-2003 10:25 PM



Advertisments