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

Hi all,
Is there anyway to use Getopt::Long without using u and p
Getopt::Long::GetOptions( 'u=s' => \$user, 'p=s' => \$passwd, );
I mean, I would like to run program using perlproram.pl username password (not perlproram.pl -u username -p password )
Thanks

Replies are listed 'Best First'.
Re: How Do I Use Getopt::Long without switches?
by ikegami (Patriarch) on Oct 06, 2007 at 07:14 UTC
    usage() if @ARGV != 2; my ($user, $passwd) = @ARGV;
Re: How Do I Use Getopt::Long without switches?
by grinder (Bishop) on Oct 06, 2007 at 12:29 UTC

    Do you mean that you want the program to use some sensible defaults if -p and -u are not given on the command-line? If that's the case, then the following snippet should help you on your way:

    Getopt::Long::GetOptions( 'u=s' => \$user, 'p=s' => \$passwd, ); $user = 'me' unless defined $user; $passwd = 'sekret' unless defined $passwd;

    If you can guarantee that neither $user nor $passwd can ever take the value 0, you can use the more compact expression:

    $user ||= 'me'; $passwd ||= 'sekret';

    ... and when perl 5.10 is released (and we're in code freeze right now) the following will work, regardless of whether the value is 0 or not.

    $user //= 'me'; $passwd //= 'sekret';

    • another intruder with the mooring in the heart of the Perl

Re: How Do I Use Getopt::Long without switches?
by FunkyMonk (Bishop) on Oct 06, 2007 at 17:33 UTC
    I'd take another look at Getopt::Long if I were you, its whole purpose is to handle command line options. If you don't need options, don't use Getopt::Long.

    I think it's @ARGV that you're looking for.

    #pm.pl print "Arg $_: $ARGV[$_]\n" for 0 .. $#ARGV;

    Sample run:

    $ perl pm.pl one two three "four five" Arg 0: one Arg 1: two Arg 2: three Arg 3: four five

Re: How Do I Use Getopt::Long without switches?
by Anonymous Monk on Oct 06, 2007 at 06:46 UTC
    @ARGV
Re: How Do I Use Getopt::Long without switches?
by dwm042 (Priest) on Oct 06, 2007 at 17:04 UTC
    I have a hard time thinking about Getopt::Long without also invoking Pod::Usage. And being an old sysadmin type, I like my documentation at the top of my programs, even if it costs me a millisecond of compile time.

    #!/usr/bin/perl use warnings; use strict; =head1 NAME param-example.pl - An example of how to use command line parms and ge +topts concurrently. =head1 SYNOPSIS param-example.pl [-u username] [-p password] [username] [password] Options: -help brief help message -man full documentation -user use name -password password =head1 VERSION author dwm042 date 10/6/2007 modified N/A =head1 DESCRIPTION param-example.pl - An example of how to use command line parms and ge +topts concurrently. This program looks first for command line parameters to define username and password and then looks for flags to define the username and password. =cut use Getopt::Long; use Pod::Usage; my $help = 0; my $man = 0; my $user = 0; my $passwd = 0; GetOptions( 'help|?' => \$help, man => \$man, "user=s" => \$user, "passwd=s" => \$passwd, ) or pod2usage(2); pod2usage( -exitval => 0, -verbose => 1 ) if $help; pod2usage( -exitval => 0, -verbose => 2 ) if $man; my $username = shift; my $password = shift; $username = $user unless defined($username); $password = $passwd unless defined($password); pod2usage(1) unless $username; pod2usage(1) unless $password;