Perl question: passing in variables

Lynn David Newton lynn.newton at gmail.com
Fri Jul 6 14:46:12 MST 2007


  v> I'm trying to run a perl script that is executable; that is, the file 
  v> has "x" permission and starts with this line:

  v> #!/usr/bin/perl -w

  v> There's a parameter in this script that's not
  v> defined by default, for example

  v> my $foo; # no default

  v> If foo is not set, the script exits with an error.  Normally I would 

What JD says about the @ARGV array is true, however
unless there is a mechanism within the perl script
itself to handle external inputs, assign it to $foo,
then there's nothing you can do to change it without
editing the script.

  v> just modify the script but this script gets
  v> extracted from a shell script that has the perl
  v> script and an rpm embedded in it. (How they did
  v> that I don't know.) So I would expect there's a
  v> way to do this, but there were no help files with
  v> the script.

  v> The shell script invokes the install file like this:
  v> ./install_script *$

I assume you mean $* not *$

The way I'm guessing this works is that it uses a here
document to drop the perl text to a temporary file and
make it executable. $* would be interpreted by the
shell as all the arguments you pass to it. For instance
if you put

echo $*

inside a shell script and execute it with arguments
you'll get something like this.

$ myscript my dog has fleas
my dog has fleas

Therefore the shell script invoking the perl script
would in turn execute

  ./install_script my dog has fleas

or whatever the same arguments are that you invoke the
shell script with.

Look inside the perl script to see if there anything
like

   $foo = shift if ! $foo;

or a line like one of these:

use Getopt::Std;
use Getopt::Long;
getopts("$someoptionstring") or die("$some_errmsg");
my $rv = GetOptions("$someoptionsstring");

If so, then the script knows how to handle input
options, which is likely (depending on where you got
this and that it works for other people.)

I'll assume that it *does* work for others, and that
therefore it does *not* simply exit if $foo is unset
with no way to set it.

my $foo;
... other code that does not deal with $foo ...
die( "blech\n" )if ! $foo;

simply must exit.

  v> So I know that I can pass one or more arguments to the outer shell 
  v> script and they will get passed verbatim to the perl script.

  v> Is there a to pass a value for foo into the script
  v> without modifying the script?

If I understand what you're saying the answer is no. 
But I have a feeling some information is missing here.

-- 
Lynn David Newton
Phoenix, AZ



More information about the PLUG-discuss mailing list