I need to include a header/boilerplate file in several Perl scripts. At this point, I'm using it as a module, but it's a big kludge. Essentially, I want the functionality that you have in C, where you can just #include the file, and it's evaluated in the scope of the file doing the #include'ing.
I've considered using: do "<file>"; but it just doesn't seem to be the best solution. This header file needs to do operations in the main:: namespace (like getting command line arguments, etc), which is why doing it as a module is not working too well. Does anybody have any suggestions?
Please reply directly to me. I am not subscribed to the list.
perldoc -f require
require filename.perl
short Summary -
Otherwise, demands that a library file be included
if it hasn't already been included. The file is
included via the do-FILE mechanism, which is
essentially just a variety of "eval". Has semantics
similar to the following subroutine: sub require {
my($filename) = @_;
return 1 if $INC{$filename};
my($realfilename,$result);
ITER: {
foreach $prefix (@INC) {
$realfilename = "$prefix/$filename";
if (-f $realfilename) {
$INC{$filename} = $realfilename;
$result = do $realfilename;
last ITER;
}
}
die "Can't find $filename in [EMAIL PROTECTED]";
}
delete $INC{$filename} if $@ || !$result;
die $@ if $@;
die "$filename did not return true value" unless $result;
return $result;
}C includes files in two stages: preprocessor and compilation; Perl "requires" files at intermediate byte-code generation -- so your require'ed file must return 1; (or true - same as using a Perl Package/module.)
the required file must have it's own my vars etc.
Creating a Perl Module/Package is best - but this will get the job done.
-- _Sx_ http://youve-reached-the.endoftheinternet.org/ _____ http://jaxpm.insecurity.org/ http://cis4dl.insecurity.org/
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>
