Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Please advise if this is a good way to make sure a parameter is supplied when my script is run in windows?
use File::Find; use strict; my $StartDir = shift || do { die "No argument given. Resubmit with parameter.\n"; exit 0; }; sub processSub { #do stuff here } find(\&processSub, $StartDir); #rest of script....
Do I also need some sort of chomp command for the data entry?
chomp $StartDir;

Replies are listed 'Best First'.
(jeffa) Re: Checking for data entry
by jeffa (Bishop) on Jul 08, 2003 at 14:40 UTC
    my $start_dir = shift || die "missing arg";
    is plenty (and no need to chomp until you actually use that parameter) , but if you have to start parsing many arguments, use a CPAN module like Getopt::Std or Getopt::Long.

    UPDATE:
    I should also add that when dealing with a starting directory, a common thing to do is supply a default instead of die'ing:

    my $start_dir = shift || '.';

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Checking for data entry
by Tanalis (Curate) on Jul 08, 2003 at 14:45 UTC
    Well .. that'd work. I'd normally use something like this, though:
    my $StartDir = shift @ARGV or die "Usage: $0 <startdir>\n";
    That way, it's immediately obvious to the user that you expect a single parameter, which is a directory to start in, and it's obvious to someone maintaining your code exactly what you expect and where it comes from. $0 simply echos the command used to run the script.

    One small bug: shift shifts from @_ by default, not @ARGV - you'll need to explictly specify @ARGV to read command line parameters.

    You won't need to chomp a parameter normally, no - the newline isn't passed into the script by the shell.

    Hope that helps.

    -- Foxcub
    #include www.liquidfusion.org.uk

    Update: Struck out an (obvious) mistake, now I think about it and it's been pointed out :) (thanks, fletcher_the_dog)

      One small bug: shift shifts from @_ by default, not @ARGV - you'll nee +d to explictly specify @ARGV to read command line parameters.
      This is not true, "shift" shifts from @ARGV unless you are inside a subroutine.
      I am kind of lost here. I only want one parameter entry not several so I would just need this?
      my $StartDir = shift || die "Usage: $0 <startdir>\n";
      and this is if I ever wanted several parameters in another script in the future I would use this?
      my $StartDir = shift @ARGV || die "Usage: $0 <startdir>\n";
        Shift just gets you the first element of the array and removes it from the array so a subsequent shift will get the next element and so on. With no array specified it uses @ARGV (or @_ in a subroutine), so the versions you give are equivalent in this case, each checking just one argument.

        Similar but NOT equivalent would be:

        my $StartDir = $ARGV[0] || die "Usage: $0 <startdir>\n";
        That checks the first command line argument but does not remove it from the argument list.

        --
        I'd like to be able to assign to an luser

        You could just evaluate the @ARGV array in scalar context and compare the result to the number of parameters you are expecting:

        @ARGV == $numExpected or die "Usage: ...\n";

        Note the use of lower priority "or" and not "||"

Re: Checking for data entry
by talexb (Chancellor) on Jul 08, 2003 at 15:29 UTC

    I would highly recommend Params::Validate to check for parameters -- positional parameters are fine until you have more than three or so, then it's lovely to be able to use a hash ref.

    --t. alex
    Life is short: get busy!