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

Hi Monks,

What I'm trying to do is take one named option from the command line and run a subroutine based on that option and use the remaining named options in the subroutine. I cannot figure out how to get the options/variables in to the subroutine. For one thing I thought the scope of the variables in Main:: would allow the variable to be seen in side the subroutine. What I'd really rather do is pass them in to the subroutine because it seems better to me. Is that true? Not to mention the fact that it seems that I'm also making this way to hard.

use strict; use warnings; use Getopt::Long; my $user; my $password; GetOptions( 'fetch' => \&fetch, 'user=s' => \$user, 'password=s' => \$password, ); sub fetch { my @vars = @_; print "We're now in fetch\n"; print "The vars are @vars\n"; print "The user is $user\n"; print "The password is $password\n"; routine1($user); } sub routine1 { my @var = @_; print "We're now in routine1\n"; print "The vars for routine1 @var\n"; print "The user for routine1 is $user\n"; }
Which produces these errors:
./mygetopt5.pl -f -user user1 -pass password We're now in fetch The vars are fetch 1 Use of uninitialized value in concatenation (.) or string at ./mygetop +t5.pl line 22. The user is Use of uninitialized value in concatenation (.) or string at ./mygetop +t5.pl line 23. The password is We're now in routine1 Use of uninitialized value in join or string at ./mygetopt5.pl line 32 +. The vars for routine1 Use of uninitialized value in concatenation (.) or string at ./mygetop +t5.pl line 33. The user for routine1 is

Thank-you for your consideration.

Replies are listed 'Best First'.
Re: Passing variables to subroutines using Getopt::Long
by lakshmananindia (Chaplain) on Mar 21, 2009 at 03:48 UTC

    Try to run the code as follows

    perl f.pl -user lakshmanan -password lakshmanan -fetch

    The above way of executing the program, worked for me and I got the following output

    We're now in fetch The vars are fetch 1 The user is lakshmanan The password is lakshmanan We're now in routine1 The vars for routine1 lakshmanan The user for routine1 is lakshmanan

    --Lakshmanan G.

    Your Attempt May Fail, But Never Fail To Make An Attempt

      Thanks, that works.

      I don't see in the docs where position on the command line matters. In fact I thought that was one benefit of Getopt::Long.

        You are telling Getopt::Long to call the sub fetch to handle the -fetch option. The sub get called at the point that the option is processed. Most likely Getopt::Long processes options in the order they are provided on the command line, although there's no reason to expect the command line options to be processed in any particular order.

        To achieve what you want I'd be inclined to call the sub after Getopt::Long has done its work. Consider:

        use strict; use warnings; use Getopt::Long; my %options; GetOptions(\%options, 'fetch', 'user=s', 'password=s'); if (exists $options{fetch}) { fetch (%options); } sub fetch { my %options = @_; print "$options{password} $options{user}\n"; }

        True laziness is hard work
        Position doesn't matter. You're using a callback to decide your program flow, but you're only supposed to use it to set variables.
        use strict; use warnings; use Getopt::Long; @ARGV = qw( --user user --password pass --fetch bob ); { my $user; my $password; my $fetch; GetOptions( 'fetch' => sub { my( $n, $k, $v ) = @_; print qq~ name = $n key = $k value = undef ~; $fetch = $k ; }, 'user=s' => \$user, 'password=s' => \$password, ); SOMETHING( $user, $password, $fetch ); die "THE END"; } sub SOMETHING { my( $user, $password, $fetch ) = @_; print qq~ user = $user password = $password fetch = $fetch ~; } __END__ $ perl f.pl name = fetch key = 1 value = undef user = user password = pass fetch = 1 THE END at f.pl line 27.