Read command-line arguments with Perl

#!/usr/bin/perl -w
use feature ':5.10';
use strict;
use warnings;
 
# How do I read command-line arguments with Perl?
 
# http://perldoc.perl.org/perlvar.html
# The array @ARGV contains the command-line arguments intended for the script.
# $#ARGV is generally the number of arguments minus one, because $ARGV[0] is
# the first argument, not the program's command name itself. See $0 for the
# command name.
 
# $0, script file name.
# @ARGV 
# $ARGV[0] .. $ARGV[9]
# $#ARGV, the last argment's subscript
# $#ARGV+1, the total 
 
 
say $0;
say @ARGV;
say $#ARGV;
 
my $numArgs = $#ARGV + 1;
 
say "you gave me $numArgs command-line arguments.";
 
for my $subscript (0 .. $#ARGV){
	say "$ARGV[$subscript]";
}
 
# @INC is include paths.
# say @INC;
for my $inc (@INC){
	say $inc;
}
 
# %ENV is the enviroment.
# say %ENV;
while ((my $key, my $value) = each %ENV) {
	say "$key=$value";
}
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">