How to get the total arguments in perl??
Hi,
How can I get the total arguments in perl.To be more specific if I try to execute the command Code:
perl -w myperl.pl ash ok kumar I know that @ARGV will store only the arguments passed but not the entire arguments. |
Quote:
Code:
$ cat /tmp/myperl.pl Code:
$ cat myperl2.pl Thanks! |
Quote:
Code:
#!usr/bin/perl -w Code:
[Ashok@station130 Assignment_1]$ perl -w package.pl ash ok kumar Code:
[Ashok@station130 Assignment_1]$ perl -w package.pl ash ok kumar |
Also, there is a special variable $0 which can be used to get our program name. But what about rest of the arguments(perl -w)?
|
Ahh. I see. The problem is that your perl script doesn't actually see the entire command line.
The shell sees Code:
perl -w package.pl ash ok kumar Code:
-w package.pl ash ok kumar Package.pl sees Code:
ash ok kumar For Package.pl to be able to see the entire command line, it's going to have to make a call back to the shell. The shell has a variable '$COMP_LINE' contains the entire command line, but it's not populated by default... I didn't have time to read the entire section in the Bash man pages about this, but I know that it's there. Alternatively, /proc/$$/cmdline is a virtual file which contains the command line. |
Code:
use warnings; |
Quote:
Hi bartonski and smeezekitty By using the virtual file /proc/$$/cmdline, I am not able to get the specific command line arguments like "perl","-w", etc..,. Rather I am getting the total command line arguments as entire string. I tried as below: Code:
#!usr/bin/perl Thanks for your replies. |
Quote:
Code:
> cat /tmp/asdf.pl |
Quote:
|
Quote:
|
The -w is a flag not an argument. The Perl interpreter takes that and modifies execution accordingly (it turns on warnings), but the -w is not supposed to end up in @ARGV.
To jump ahead a little bit, can you tell us more about what you are really trying to do? If your goal is to be able to write command-line scripts with flags, Perl has lots of good modules to help you do that. Take a look at GetOpt::Std or Getopt::Long. |
Quote:
To get the number of args just run $#ARGV, btw. |
Quote:
|
Another thing I'd point out is... you should really be specifying the commandline uses in the script itself--
Code:
use strict; You can see an example of this like such: Code:
perl -e 'print $0 . " has " . (($#ARGV)+1) . " args.\n"' Code:
#!/usr/bin/perl Code:
core:~/test/test22$ perl -e 'print $0 . " has " . (($#ARGV)+1) . " args.\n"' 1 2 3 |
Quote:
for( $val= 0; $val <= $#ARRAY; $val++ ){ } Which is just a C habit. However, it's all about preference, and both will work. Yay Perl :) |
All times are GMT -5. The time now is 09:22 AM. |