1

How to use command line argument in Perl to take input a file from user? May be like: wxp.pl - file {file path}.
I am new one in Perl so suggest me anything which is helpful. And how can I make it like more then one arguments to ask in command line. ex:

exp.pl -file {file path}
           -x    {it's arg}
           -y    {y's arg}

3 Answers 3

2

If simple you want pass arguments, you can do like this:

perl exp.pl arg1 agr2

If you want something more, use Getopt::Long module.

Sign up to request clarification or add additional context in comments.

2 Comments

Ohhk thank you i have to look that up Getopt::Long because i have no idea how to do that New into the perl you know.
Just have a look and try to do from yourself, and if you face any problem come here with your attempts. We glad to help you.
1

I would suggest GetOpt::Long to achieve that. But if you want to make your own see if something like this would be handy:

use strict;
use warnings;

# Usage check. For "perl xxx.pl -a arg1 -b arg2" args. should be even nos. 

if (scalar @ARGV == 0 or scalar @ARGV % 2 != 0) {
    print "Usage:\n\t$0 -a arg1 -b arg2 requires all args\n";
    exit 0;
}

# This one convert cmd line args. to hash ex. (perl xxx.pl -a arg1 -b arg2) to {-a => arg1, -b => arg2}
my %argHash = @ARGV;    

# Optional but see if all the args are present
my $maxArgs = 2;
if (scalar keys %argHash != $maxArgs) {
    print "Usage:\n\t$0 -a arg1 -b arg2 requires exactly $maxArgs args\n";
    exit 0;
}

# Parse arguments. 
print "$0:\n";
for my $key (keys %argHash) {
    print "\t $key: $argHash{$key}\n";  
}

1;

Result:

c:\swadhi\perl>perl cmdline.pl
Usage:
        cmdline.pl -a arg1 -b arg2 requires all args

c:\swadhi\perl>perl cmdline.pl -a "user" -b
Usage:
        cmdline.pl -a arg1 -b arg2 requires all args

c:\swadhi\perl>perl cmdline.pl -a "user" -b "Command"
cmdline.pl:
         -a: user
         -b: Command

c:\swadhi\perl>perl cmdline.pl -a "user" -b "Command" -c "mock"
Usage:
        cmdline.pl -a arg1 -b arg2 requires exactly 2 args

c:\swadhi\perl>perl cmdline.pl -a "user"
Usage:
        cmdline.pl -a arg1 -b arg2 requires exactly 2 args

Comments

1

More simpler GetOpt::Long version

use strict;
use warnings;
use Getopt::Long qw(GetOptions);

my $user_name;

GetOptions('user=s' => \$user_name) or die "Usage: $0 --user NAME\n";

if ($user_name) {
    print  $user_name, "\n";
}

Result:

c:\swadhi\perl>perl cmdline.pl -user Swadhi
Swadhi

c:\swadhi\perl>perl cmdline.pl

c:\swadhi\perl>perl cmdline.pl -p s
Unknown option: p
Usage: cmdline.pl --user NAME

c:\swadhi\perl>perl cmdline.pl -u "This should also work"
This should also work

2 Comments

well i will try to use this one in my solution and thanks.Though i am working on a email module in which i want to enter a email ids as an argument so with that it will send a mail to those id and i am using mail::outlook for that.
Sure. I would recommend that you practice it for fuller understanding and left it out with very simple example. Cheer! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.