in reply to Re: Checking for data entry
in thread Checking for data entry

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";

Replies are listed 'Best First'.
Re: Re: Re: Checking for data entry
by Albannach (Monsignor) on Jul 08, 2003 at 15:58 UTC
    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

      thanks for the info. So this doesnt have a bug?
      my $StartDir = shift || die "Usage: $0 <startdir>\n";
        Well it will do what you want and I use it all the time for my little scripts, but it is not particluarly maintenance friendly. What if someone inserts another shift before yours for example? How robust you want your code to be will determine how you code this (and everything else too really).

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

Re: Re: Re: Checking for data entry
by Anonymous Monk on Jul 08, 2003 at 18:06 UTC
    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 "||"