sravani has asked for the wisdom of the Perl Monks concerning the following question:

i am prompting for input on the screen for two args, instead of prompting for input how can i take the 2 args from the command line itself means (./name arg1 arg2) like this!!
use strict; use warnings; print "gate1 name \n"; my $gate1 = ''; chomp($gate1 = <STDIN>); print "gate2 name \n"; chomp($gate2 = <STDIN>);

Replies are listed 'Best First'.
Re: command line arguments
by graff (Chancellor) on Nov 05, 2007 at 01:16 UTC
    You want to know about @ARGV:
    use strict; use warnings; my ( $gate1, $gate2 ); if ( @ARGV == 2 ) { ( $gate1, $gate2 ) = @ARGV; } else { die "Usage: $0 gate1_name gate2_name\n"; } # no need to chomp @ARGV values...
    (update: changed to declare the "gate" variables outside the "if", so they would be available for use outside the if block)
Re: command line arguments
by jdporter (Paladin) on Nov 05, 2007 at 01:16 UTC

    The arguments given on the command line are available in the special global variable @ARGV. If you need more power/flexibility in commandline processing, you could consider modules such as Getopt::Std and Getopt::Long (both of which are in the standard library).

    A word spoken in Mind will reach its own level, in the objective world, by its own weight
Re: command line arguments
by megaurav2002 (Monk) on Nov 05, 2007 at 05:43 UTC
    Hi Sravani
    The following takes a single argument from command line and shows an usage message upon wrong input

    use Getopt::Long; sub usage { print "\nUsage: thisProgram -f <expectedUserInput>\n"; exit(1); } GetOptions("f=s=>\$userInput"); &usage() if (!defined($userInput));
    Cheers,
    Gaurav
    "Wisdom begins in wonder" - Socrates, philosopher