Welcome to WebmasterWorld Guest from 54.157.222.62

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Perl require or use

   
10:22 am on Feb 7, 2006 (gmt 0)

5+ Year Member



Hi all
I have a perl file, in it I declared a function, from another file I want to call this function, how I must write?

Thanks....

10:43 am on Feb 7, 2006 (gmt 0)

5+ Year Member



you could use "use" or "require". What have you tried so far?
10:47 am on Feb 7, 2006 (gmt 0)

5+ Year Member



I have perl file with name "file_name" and in it I have function with "my_function"
I used require like this

require "file_name";

Perl_code.................
..........
and then

my_function;

There is no result :(

11:09 am on Feb 7, 2006 (gmt 0)

10+ Year Member



If the included perl file just contains functions then I use

do "includefile.pl";

11:31 am on Feb 7, 2006 (gmt 0)

5+ Year Member



How can I do it? can you show me brief introduction?
11:52 am on Feb 7, 2006 (gmt 0)

10+ Year Member



If the file main.pl wants to include functions from subs.pl then the top of main.pl simply looks like

#!/usr/bin/perl 

do "subs.pl";

12:06 pm on Feb 7, 2006 (gmt 0)

5+ Year Member



Ok. Thank you very very much dear!
7:56 pm on Feb 7, 2006 (gmt 0)

5+ Year Member



mainscript, call it main.pl:


#!perl
use strict;
use CGI qw/:standard/;;
require "my_subs.pl";

print header;
my $digits = param('digits') 100;
my $incremented = add($digits);
print $incremented;

my_subs.pl (in same folder as main.pl)


use strict;
sub add {
my $number = shift;
$number++;
return($number);
}
1;

note the last line of my_subs.pl, this is important as required files have to return a true value when first called with "require".

You can use the "do" function (not really a function but I don't know what it's called), but in the "do" documentation it says:

Note that inclusion of library modules is better done with the use() and require() operators, which also do automatic error checking and raise an exception if there's a problem.

Note: the is supposed to be the pipe character which is on the same key as the back-slash charater \ , this forum changes it for some reason.

7:58 pm on Feb 7, 2006 (gmt 0)

5+ Year Member



note that my_subs.pl has no path to perl line as the first line, it's not needed although you can include it.
8:27 pm on Feb 7, 2006 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Doesn't "use" require that the included module be a formal package, adhering to exported package specs?

In any case, I read long ago "use" is preferred over require, but I use use "require" for all my includes.

Oh my, look at that, use require, require use . . .

9:35 pm on Feb 7, 2006 (gmt 0)

5+ Year Member



according to the perl documentation, using "use" is the same as:


BEGIN { require Module; import Module LIST; }
except that Module must be a bareword.

for all the dirty details:

[perldoc.perl.org...]