ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 should be able to get all the command line arguments.
I know that @ARGV will store only the arguments passed but not the entire arguments.
I'm not quite sure what you mean by "entire arguments" as opposed to "arguments passed". Are you looking for the number of arguments passed?
Code:
$ cat /tmp/myperl.pl
#!/usr/bin/perl
use strict;
use warnings;
print join(" ", @ARGV) . "\n";
print scalar @ARGV . "\n";
$ perl /tmp/myperl.pl one two three four
one two three four
4
If you are trying to get environment variables exported by the shell, try this:
Code:
$ cat myperl2.pl
#!/usr/bin/perl
use strict;
use warnings;
print "the value of the environment variable \$USER is '$ENV{USER}'\n";
$ perl myperl2.pl
the value of the environment variable $USER is 'tiger'
Please explain what you mean by "entire arguments" as opposed to "arguments passed".
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
perl sees
Code:
-w package.pl ash ok kumar
[and knows that it is being called as 'perl'].
Package.pl sees
Code:
ash ok kumar
[and knows that it is being called as 'Package.pl']
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.
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.
How can I invoke $COMP_LINE correctly in perl?
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
use strict;
use warnings;
open F, "/proc/$$/cmdline";
my @a=split "", <F>;
print @a,"\n";
close F;
How can I get the specific command for example "-w".
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
use strict;
use warnings;
open F, "/proc/$$/cmdline";
my @a=split "", <F>;
print @a,"\n";
close F;
How can I get the specific command for example "-w".
Thanks for your replies.
I ran smeezekitty's code, and /proc/$$/cmdline does in fact contain the '-w'. Smeezekitty's code splits the file into individual characters. If you want to see it by words, you have to use ascii 0 as the delimiter.
Code:
> cat /tmp/asdf.pl
#!usr/bin/perl
use strict;
use warnings;
open F, "/proc/$$/cmdline";
my @a=split "\000", <F>;
print "@a\n";
close F;
> perl -w /tmp/asdf.pl asdf asdf sadf sadf
perl -w /tmp/asdf.pl asdf asdf sadf sadf
How can I get the specific command for example "-w".
Why would you want to? If you're trying to detect whether perl is running -w so you can modify your own warnings behavior, you should just have your perl script accept -w as well.
Why would you want to? If you're trying to detect whether perl is running -w so you can modify your own warnings behavior, you should just have your perl script accept -w as well.
In general, I have to agree. If your perl script needs to resort to tricks like this, you probably need to revisit the design of your program. If you can't see a way around this on your own, you might also want to talk to someone about the design its self.
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.
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.
Seconded - GetOpt is the right way to handle complex args.
To get the number of args just run $#ARGV, btw.
Another thing I'd point out is... you should really be specifying the commandline uses in the script itself--
Code:
use strict;
use warnings;
There is really no time you don't want those on. $0 will return perl and it's full path and as someone said earlier $#ARGV+1 will return the number of *arguments*, but *perl* flags aren't arguements. I don't recall a way to grab what perl flags are in use however.
not quite- that'll be the number of args - 1, since ARGV[0] is the first argument (unlike C) You want scalar(@ARGV)
Sure - that's true that it gives you the size of an array counting from 0. Both give you the size, though they give you a different value Best part of perl is that there are many ways to shoot off your foot. I happen to prefer arrays 0 based, so that I can walk through it like this:
for( $val= 0; $val <= $#ARRAY; $val++ ){
}
Which is just a C habit.
However, it's all about preference, and both will work. Yay Perl
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.