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

hi I am trying to pass argument through command line like this
perl report.pl -CustomerName Penelope NY > test.txt
Here Penelope is the name of customer and NY is the location So for -CustomerName option I have to pass two or more than arguments without taking any other option for NY .. Please help me out for this problem

Replies are listed 'Best First'.
Re: Problem in passing multiple argument through command line
by Corion (Patriarch) on Nov 13, 2007 at 09:45 UTC

    This seems to be more an shell issue than a Perl issue. You need to properly quote arguments that contain whitespace in your command line:

    perl report.pl -CustomerName "Penelope NY" > test.txt

    As a matter of style, I recommend using Getopt::Long, possibly together with Pod::Usage to create the command-line handling parts for scripts. A detailed discussion is at The Dynamic Duo --or-- Holy Getopt::Long, Pod::UsageMan!, by PodMaster.

    With that, your command line would have to look like this:

    perl report.pl --CustomerName "Penelope NY" > test.txt

    And the command line handling code in your program would be:

    use strict; use Getopt::Long; use Pod::Usage; GetOptions( 'CustomerName' => \my @customers, ... ); for my $customer (@customer) { print "Hello $customer!\n"; };
Re: Problem in passing multiple argument through command line
by cdarke (Prior) on Nov 13, 2007 at 09:47 UTC
    Or maybe rethink your design. Should it be:
    perl report.pl -CustomerName Penelope -Location NY?
Re: Problem in passing multiple argument through command line
by brian_d_foy (Abbot) on Nov 13, 2007 at 10:41 UTC

    Chapter 11 of Mastering Perl is all about configuring Perl programs, including command line parsing and options.

    You haven't provided any code, so we can't see what's actually happen (and thus help you). I'm not sure which module allows long switches with single hyphens and a space between the value either. Typically, however, the command line parsers parse what they can and leave the rest in @ARGV. Check some of the examples in Mastering Perl to see if one of them is what you need.

    And, post a small test program the demonstrates your problem. Include what you expected it to do and what it actually does. :)

    Good luck, :)

    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review
Re: Problem in passing multiple argument through command line
by narainhere (Monk) on Nov 13, 2007 at 11:23 UTC
    How about this
    use strict; use warnings; use Getopt::Long; my $location; my $name; if(!GetOptions( "name", "location=s"=> \$location ) ) { print "\nUnknown options have been provided\n"; } $name=join(" ",@ARGV); print "Name is $name and location is $location";
    But you should be careful that you pass only name and location, not anything else otherwise those junk would show up as names An sample command line would look like
    perl 650479.pl -name Ram Sam Dam Yam -location Germany

    update :Added a sample command line

    The world is so big for any individual to conquer

Re: Problem in passing multiple argument through command line
by naikonta (Curate) on Nov 14, 2007 at 02:10 UTC
    I prefer to use existing modules as shown by others. For simple options, however, I use the -s switch a lot.
    $ cat clo.pl #!/usr/bin/perl -s use strict; use warnings; use vars qw($company $location); $company ||= 'NO_COMPANY'; $location ||= 'NO_LOCATION'; printf "The %s company is located at $location\n", $company, $location; $ ./clo.pl The NO_COMPANY company is located at NO_LOCATION $ ./clo.pl -company='Grant Alpho System' The Grant Alpho System company is located at NO_LOCATION $ ./clo.pl -company='Grant Alpho System' -location=TheOtherGlobe The Grant Alpho System company is located at TheOtherGlobe

    Or, if you only want to read a location with more customers, you can read from @ARGV yourself.

    $ cat report.pl #!/usr/bin/perl use strict; use warnings; my($location, @customers) = @ARGV; die "Can't create report, no customer defined\n" unless @customers; print "The following customers are located at $location\n"; my $i; for (@customers) { print ++$i, ". $_\n"; } $ ./report.pl Can't create report, no customer defined $ ./report.pl NY Penelope Abraham Marie 'John Doe' The following customers are located at NY 1. Penelope 2. Abraham 3. Marie 4. John Doe

    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!