http://www.cyberciti.biz/faq/howto-pass-perl-command-line-arguments/
How do I read or display command-line arguments with Perl?
Perl command line arguments stored in the special array called @ARGV.
ARGV example
Use $ARGV[n] to display argument.
Use $#ARGV to get total number of passed argument to a perl script.
For example, if your scriptname is foo.pl:
./foo.pl one two three
You can print one, two, three command line arguments with print command:
print "$ARGV[0]\n"; print "$ARGV[1]\n"; print "$ARGV[2]\n";
Or just use a loop to display all command line args:
#!/usr/bin/perl -w if ($#ARGV != 2 ) { print "usage: mycal number1 op number2\neg: mycal 5 + 3 OR mycal 5 - 2\n"; exit; } $n1=$ARGV[0]; $op=$ARGV[1]; $n2=$ARGV[2]; $ans=0; if ( $op eq "+" ) { $ans = $n1 + $n2; } elsif ( $op eq "-"){ $ans = $n1 - $n2; } elsif ( $op eq "/"){ $ans = $n1 / $n2; } elsif ( $op eq "*"){ $ans = $n1 * $n2; } else { print "Error: op must be +, -, *, / only\n"; exit; } print "$ans\n";
Save and run script as follows:$ chmod +x mycal.pl
$ ./mycal.pl
$ ./mycal.pl 5 + 3
$ ./mycal.pl 5 \* 3
Note: * need to be escaped under UNIX shell.
Page last updated at 1:47 AM, January 7, 2012.
'Programming' 카테고리의 다른 글
(Java) Execute Shell Command From Java (0) | 2012.06.10 |
---|---|
How do I download a file using Perl? (0) | 2012.06.08 |
(C#) Play Sound Files using libZPlay (0) | 2012.06.07 |
libZPlay: Easy way to play mp3, ogg, ac3, flac, aac, wav and pcm files in your Win32 application (0) | 2012.06.07 |
(C++) Print Hex (0) | 2012.06.06 |