Re: Passing a required script arguments
by gjb (Vicar) on Sep 26, 2003 at 15:13 UTC
|
Have a look at the system function, e.g. system('foo.pl', 'par1', 'par2').
Hope this helps, -gjb-
| [reply] [d/l] [select] |
Re: Passing a required script arguments
by jdtoronto (Prior) on Sep 26, 2003 at 15:45 UTC
|
If you require foo.pl in that way it is as though you typed it right there. Don't forget that the file must evaluate true, so put a 1; after the last closing } at the end of the file.
Because you just typed the subs in foo.pl right there, then you call the subs within it as normal. so, if the sub dothis has been declared in foo.pl - just call it as usual - $rc = dothis( par1, par2 );
You can do all sorts of other stuff, but this is the most basic way of 'including' code from another file. I used this technique a few years back to build hte functional blocks of a large CGI based web-app. Based on the function selected from the menu I couild require the code for that group of functions and then pass the parameters that came back from the CGI.
You are now on the way to your first Perl module! Have fun and look at the documents here regarding modules, it is in fact veryt simple to make a custom module of your own which will make maintenance easier and help with issues of namespace. But that's for another time!
jdtoronto | [reply] |
|
|
| [reply] |
Re: Passing a required script arguments
by sgifford (Prior) on Sep 26, 2003 at 15:18 UTC
|
When you require a file, it's exactly (well, almost exactly) as if you'd just typed those lines right where you put the require statement. Because of this, there isn't really a way to pass arguments.
If what you really want to do is run the script in a new process, just use system as gjb suggested.
If you actually want to require the script, perhaps so you can use its subs or look at its variables, you can pass parameters in a global variable. If the script is expecting parameters in the @ARGS array, just set @ARGS, possibly using local if you want access to the calling program's original copy of @ARGS.
| [reply] [d/l] [select] |
|
|
here are my two simple test scripts:
#####bar.pl#####
#!/perl/bin/perl
use warnings;
use strict;
require "./foo.pl";
&foo('test', 'this');
#####foo.pl######
#!/perl/bin/perl
my @args = @_;
foreach (@args) {
print "ARGS: $_\n";
}
| [reply] [d/l] |
Re: Passing a required script arguments
by Abigail-II (Bishop) on Sep 26, 2003 at 15:16 UTC
|
Huh?
How do you intent to process the arguments?
Something is wrong with your perception of how Perl works.
If you want to call a program (with or without arguments),
use system, not require. If you want to load a module, and
give that some arguments, use "use", like:
use Module "arg1", "arg2", "arg3";
the arguments are then passed on to the import() subroutine.
Abigail | [reply] [d/l] |
|
|
As you well know, when you require a file, the code in the file is executed. Given that, what's "wrong with [his] perception of how Perl works"? Sounds to me like he just wants to wrap an existing script that uses @ARGV and that's perfectly doable as long as the script doesn't explicitly exit or die. He can do preprocessing on @ARGV as well as additional processing after the require'd code...
echo 'sub foo { print for @ARGV } foo(); 1;' > foo.pl
perl -le 'require "foo.pl"; foo()' bar baz
Not that I'd argue such a thing is wise, but it is doable and therefore thinking it is doable doesn't indicate a flawed understanding of how Perl works.
-sauoq
"My two cents aren't worth a dime.";
| [reply] [d/l] |
|
|
As you well know, when you require a file, the code in the file is executed.
Is it?
for (1 .. 10) {
require "./foo.pl";
}
executes "foo.pl" at most once, although it is required
10 times.
Abigail | [reply] [d/l] |
|
|
|
|
A reply falls below the community's threshold of quality. You may see it by logging in.
|