I hate having to send the same parameters to a script but there are times when you need to vary from your defaults. I came up with this code (using CGI to handle parameter parsing) with the help of a www.perlguru.com user: mhx. You can define a default for your parameters and if the user supplies a value under the same argument name, the users value is used; otherwise your default is used. Insert into your own code if you need this sort of capability. P.S I could only get 'join "&" => @ARGV' to work this way for @ARGV handling even though CGI shuold deal with it directly. Enjoy!
#!/usr/bin/perl # define script defaults (change these to suit your needs or supply th +em in your script calls) my %defaults = ( foo => "bar", bar => "foo", ... => ..., ); # to handle parameter passing use CGI; # get the script parameters; 1st from the ARGV array (in the case of S +SI calls) then the QUERY_STRING if (@ARGV) { $input = new CGI ( join "&" => @ARGV ); } else { $input = + new CGI; } # set the values for parameters omitted by user to the %defaults value +s for ( keys %defaults ) { $input->param(-name=>$_,-value=>$defaults{$_} +) unless $input->param($_); } ######### # the rest of your program here #########

Replies are listed 'Best First'.
Re: script call over-ridable defaults
by particle (Vicar) on Mar 11, 2002 at 16:42 UTC
    instead, i'd use something like this, as is referenced (sort of) in the CGI documentation:

    #!/usr/local/bin/perl -wT use strict; $|++; use CGI; my %defaults = ( foo => "bar", bar => "baz", ); my $input = CGI->new(); # gimme CGI! $input->use_named_parameters(1); # tell CGI to add dashes # remember to untaint @ARGV here!!! $input->param( %defaults, @ARGV ); # assign params after untainting # more code...
    don't be so trusting of @ARGV. make sure it is untainted before you use it!

    ~Particle ;Þ