Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: how to deal with incorrect command line argument

by Kenosis (Priest)
on Oct 30, 2013 at 21:58 UTC ( [id://1060456]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1060456]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-04-19 15:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found