Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Perl Input Parameters for Unix

by aitap (Curate)
on Jul 09, 2012 at 16:11 UTC ( [id://980713]=note: print w/replies, xml ) Need Help??


in reply to Perl Input Parameters for Unix

Other than processing options using Getopt::Long (which I use most frequently) and Getopt::Std, options can be parsed manually. For example, in your case:
my ($input,$stat,$csv); while (my $arg = shift @ARGV) { if ($arg eq "-i") { $input = shift @ARGV; } elsif ($arg eq "-s") { $stat = shift @ARGV; } elsif ($arg eq "-o") { $csv = shift @ARGV; } else { die "$arg: invalid argument\n"; } defined $input && defined $csv && defined $stat || die "You should def +ine -i, -o and -s!\n";
You can also use 5.010 and process arguments in "switch-case" style.
Sorry if my advice was wrong.

Replies are listed 'Best First'.
Re^2: Perl Input Parameters for Unix
by monsoon (Pilgrim) on Jul 09, 2012 at 16:31 UTC
    When you parse the options manually, you'll also have to manually deal with multiple single letter options bundled into one argument. Something like -abc - that's 3 switches "a", "b" and "c". In fact "c" can even have an argument. You don't have to worry about any of this stuff with Getopt::Std.
Re^2: Perl Input Parameters for Unix
by Anonymous Monk on Jul 10, 2012 at 04:49 UTC

    Could you explain in detail how this works exactly? Thank you

      This can work because there is an array called @ARGV, which contains all the command line arguments passed to the perl script (not to the perl itself). See perlvar for more.

      `shift` removes and returns the first element of this array, until none are left (when array is empty, it returns undef), so this `while` loop checks all the command line arguments from left to right (additional `shift`s inside the loop are used to get the parameter next to being processed now). See shift for more.

      Sorry if my advice was wrong.

Log In?
Username:
Password:

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

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

    No recent polls found