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
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
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.
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>
> 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!
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 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).
>> #!/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
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.
That, however, would be caught at compile time, so the presence or
absence of "die" wouldn't make a difference.
hp
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.