in reply to Re^2: Passing variables to subroutines using Getopt::Long
in thread Passing variables to subroutines using 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"; }
|
|---|