in reply to how to deal with incorrect command line argument

Perhaps the following will be helpful:

use strict; use warnings; my @array; for (@ARGV) { if (-e) { push @array, $_; next; } warn qq{"$_" doesn't exist!}; } @ARGV = @array; print "@ARGV";

This pushes the 'good' params onto @array and warns on the others, then reinitializes @ARGV with the 'good' ones.

Replies are listed 'Best First'.
Re^2: how to deal with incorrect command line argument
by marinersk (Priest) on Oct 31, 2013 at 00:23 UTC
    Slightly simplified, I think:

    use strict; use warnings; for (@ARGV) { if (-e) { &doTheWork($_); } else { warn qq{"$_" doesn't exist!}; } }

      Good suggestion! Or even just:

      use strict; use warnings; (-e) ? doTheWork($_) : warn qq{"$_" doesn't exist!} for @ARGV;
        Nice!

        I think that officially crosses from stuff-a-noob-can-read to stuff-that-makes-noob-heads-spin, though.  :-)

Re^2: how to deal with incorrect command line argument
by scripter87 (Novice) on Oct 30, 2013 at 22:28 UTC
    Thanks my man, does the trick. May I ask a few questions on your solution though... I am going to comment it on your code and maybe you can correct me where I am wrong.
    my @array; # this an array to store the good arguments for (@ARGV) { # for the argumets array if (-e) { # if the file exists push @array, $_;# push the file that exists onto the @array # why use $_ here? next; # what purpose has this } warn qq{"$_" doesn't exist!}; } @ARGV = @array;
    I Appreciate the time you took to answer my question to man.

      You're most welcome! Am glad it helped.

      You're commenting is correct. You asked, "why use $_ here?" Because Perls default scalar ($_) is implicitly used in the for loop that's iterating through the elements of @ARGV. Note, also, that Perl's default scalar is also implicitly used in the expression if (-e) { as that expression is equivalent to if (-e $_) {.

      The purpose of next is to get the next element of @ARGV, otherwise a warn would occur.