homepage Welcome to WebmasterWorld Guest from 174.129.163.183
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Perl require or use
Ejdaha




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

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

 

perl_diver




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

you could use "use" or "require". What have you tried so far?

Ejdaha




msg:432333
 10:47 am on Feb 7, 2006 (gmt 0)

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

uncle_bob




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

If the included perl file just contains functions then I use

do "includefile.pl";

Ejdaha




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

How can I do it? can you show me brief introduction?

uncle_bob




msg:432336
 11:52 am on Feb 7, 2006 (gmt 0)

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


Ejdaha




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

Ok. Thank you very very much dear!

perl_diver




msg:432338
 7:56 pm on Feb 7, 2006 (gmt 0)

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.

perl_diver




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

note that my_subs.pl has no path to perl line as the first line, it's not needed although you can include it.

rocknbil




msg:432340
 8:27 pm on Feb 7, 2006 (gmt 0)

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

perl_diver




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

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved