Monday, 5 August 2019

How to pass command line Arguments to a perl script using multiple methods?


Method1:

perl tmp.pl 1 2 7 3 5
($a,$bcsa,$msx, $flag ,$key) = @ARGV;

Method2:

while (my $var = <>) {
  print $var;
}

Method3:

while (my $var1 = shift) {
  print "$var1\n";
}

Method6:
foreach my $argnum (0 .. $#ARGV) {

   print "$ARGV[$argnum]\n";

}

Method7:
foreach my $arg (@ARGV) {
    print $arg, "\n";
}

Method5:

If the arguments are options/switches, use GetOpt::Std or GetOpt::Long,
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.

kaavannan perl blogspot

Labels: , , , ,

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home