Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

How do you use command line parameters?

by Zombie BlackShift (Initiate)
on Sep 20, 2000 at 18:30 UTC ( #33287=categorized question: print w/replies, xml ) Need Help??
Contributed by Zombie BlackShift on Sep 20, 2000 at 18:30 UTC
Q&A  > input and output

Answer: How do you use command line parameters?
contributed by japhy

Command-line parameters are sent to a Perl program in the same way they're sent to any other.

% ./myprog foo bar blat
The @ARGV array holds the command-line arguments, and in this case, it would hold ('foo', 'bar', 'blat').

Perl allows for simplistic command-line options via the -s option to perl:
#!/usr/bin/perl -s print "value of -x: $x\n"; print "value of -name: $name\n";
Here's a sample run:
% ./myprog -x -name=Jeff value of -x: 1 value of -name: Jeff
If you want more complex option parsing, there are two standard modules that can do this for you: Getopt::Std and Getopt::Long.

Perl allows you to treat the arguments in @ARGV as filenames, by using the special case of the <> operator.
while (<>) { # $ARGV is the filename # $_ is the line # $. is the line number # reset it to 0 by doing # $. = 0 if eof; # or # close ARGV if eof; }
Answer: How do you use command line parameters?
contributed by Zombie BlackShift

#!/usr/bin/perl foreach (@ARGV) { print $_; };
Answer: How do you use command line parameters?
contributed by DigitalKitty

The easiest way is to use the Getopt::Std module ( it is part of the standard distribution ). Here is a sample program.

#!/usr/bin/perl -w use strict; use Getopt::Std; use vars qw( $opt_s $opt_r $opt_n ); if( ! getopts('srn' ) ) { die "Usage: -srn filename\n"; #Example: perl -r test.txt } my @file = <>; if( $opt_s ) { @file = sort { $a cmp $b } @file; } if( $opt_r ) { @file = reverse @file; } if( $opt_n ) { @file = sort { $a <=> $b } @file; }

Each command-line switch is assigned to its respective scalar variable ( $opt_s, etc. ) and if it exists, its individual value is 1.
You can also use multiple switches:
$perl -rn test.txt # Reverse the contents of test.txt and perform # a numeric sort on it.

# Sample runs: C:\perl>perl -r test.txt third line. second line. first line. C:\perl> C:\perl>perl -n test.txt 2 33 33 44 48 55 889 990 C:\perl> C:\perl>perl -c test.txt Unknown option: c Usage: -srn filename C:\perl>

In the last example, I hadn't declared an $opt_c in the line 'use vars qw ( $opt_s ... );' or an if() statement to handle it. This is why I received the message.

Hope this helps,

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [marto]: good morning all

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (6)
    As of 2017-06-28 07:14 GMT
    Find Nodes?
      Voting Booth?
      How many monitors do you use while coding?

      Results (628 votes). Check out past polls.