(Note that doing this with shift will only work if you are outside of all subs. Within a sub, it will retrieve the list of arguments passed to the sub rather than those passed to the program.)
Depends on what you want to do. If you want to use the two arguments as input files, you can just pass them in and then use <> to read their contents.
If they have a different meaning, you can use GetOpt::Std and GetOpt::Long to process them easily. GetOpt::Std supports only single-character switches and GetOpt::Long is much more flexible. From GetOpt::Long:
use Getopt::Long;
my $data = "file.dat";
my $length = 24;
my $verbose;
$result = GetOptions ("length=i" => \$length, # numeric
"file=s" => \$data, # string
"verbose" => \$verbose); # flag
Alternatively, @ARGV is a special variable that contains all the command line arguments. $ARGV[0] is the first (ie. "string1" in your case) and $ARGV[1] is the second argument. You don't need a special module to access @ARGV.
Yet another options is to use perl -s, eg:
Then call it like this :
Or see the original article for more details:
You can access them directly, by assigning the special variable
@ARGV
to a list of variables. So, for example:perl tmp.pl 1 2 3 4 5
If the arguments are filenames to be read from, use the diamond (<>) operator to get at their contents:
If the arguments are options/switches, use GetOpt::Std or GetOpt::Long, as already shown by slavy13.myopenid.com.
On the off chance that they're something else, you can access them either by walking through @ARGV explicitly or with the
shift
command:(Note that doing this with
shift
will only work if you are outside of allsub
s. Within asub
, it will retrieve the list of arguments passed to thesub
rather than those passed to the program.)will print each argument.
Depends on what you want to do. If you want to use the two arguments as input files, you can just pass them in and then use
<>
to read their contents.If they have a different meaning, you can use
GetOpt::Std
andGetOpt::Long
to process them easily.GetOpt::Std
supports only single-character switches andGetOpt::Long
is much more flexible. FromGetOpt::Long
:Alternatively,
@ARGV
is a special variable that contains all the command line arguments.$ARGV[0]
is the first (ie."string1"
in your case) and$ARGV[1]
is the second argument. You don't need a special module to access@ARGV
.