<Help> How to use routines from another perl script

B

baiyanhuang

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

Jürgen Exner

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
 
P

Petr Vileta \(fidokomik\)

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');
 
T

Tim Greer

Petr said:
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.
 
P

Petr Vileta \(fidokomik\)

Tim said:
^ Down there, maybe --> ?

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

use strict;
no warnings;

:)

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

Tim Greer

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



use strict;
no warnings;

:)


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).
 
H

Hans Mulder

Tim said:
Petr Vileta (fidokomik) wrote:

use warnings;


|| die, or die, or use eval.

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

Were you thinking of "do", perhaps?

-- HansM
 
T

Tim Greer

Hans said:
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.
 
P

Peter J. Holzer

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
 
T

Tim Greer

Peter said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top