Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

perl problem using "getopts"

Options
  • 05-02-2007 6:28pm
    #1
    Registered Users Posts: 4,359 ✭✭✭


    hi,

    have a mystery here i am using the Getopts pm to read in a few command line arguments,(see code snippet) now it works fine on one unix server, but on another (idendically spec'd sun solaris) server it reads nothing
    #!/usr/bin/perl
    use strict;
    use Getopt::Std;
    
    
    
    
    ### Declare Command line arguments ####
    our %opts;
    getopts('n:t:s:', \%opts);
    
    our $opt_n;
    our $opt_t;
    print "$opt_n\n";  #test if value is being read in 
    our $opt_s;
    my $number = $opt_n;
    my $target = $opt_t;
    my $source = $opt_s;
    exit;
    
    #########################################################
    output:
    
    $>perl test_options.pl -n 1
    (nothing)
    
    or
    
    $>perl -w test_options.pl -n 1
    Use of uninitialized value in concatenation (.) or string at test_options.pl line 14.
    

    any ideas? the exact same code works on another identical server


Comments

  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    $ perldoc -m Getopt::Std
    ...snip..
        use Getopt::Std;
    
        getopt('oDI');    # -o, -D & -I take arg.  Sets $opt_* as a side effect.
        getopt('oDI', \%opts);    # -o, -D & -I take arg.  Values in %opts
        getopts('oif:');  # -o & -i are boolean flags, -f takes an argument
                          # Sets $opt_* as a side effect.
        getopts('oif:', \%opts);  # options as above. Values in %opts
    ...snip
    
    Your use is similar to the last form where the values are put into the %opts hash.
    You can either change the getopts() line to
    getopts('n:t:s:');
    
    or reference $opts{'n'} instead of $opt_n.

    I don't understand how it works on one machine.
    Aside, why the mixture of 'our' and 'my'?


  • Registered Users Posts: 4,359 ✭✭✭jon1981


    yes tis very strange, this worked and didnt use hash. I used our because i was using "use strict"
    #!/usr/bin/perl
    use strict;
    use Getopt::Std;
    
    
    
    
    ### Declare Command line arguments ####
    
    
    getopts('n:t:s:');
    
    our $opt_n;
    print "$opt_n\n";  #test if value is being read in
    my $number = $opt_n;
    print "$number\n";
    our $opt_t;
    print "$opt_t\n";  #test if value is being read in
    my $number1 = $opt_t;
    print "$number1\n";
    exit;
    


    output

    $ perl -w test_user.pl -n 1 -t 2
    1
    1
    2
    2


  • Registered Users Posts: 6,509 ✭✭✭daymobrew


    jon1981 wrote:
    yes tis very strange, this worked and didnt use hash. I used our because i was using "use strict"
    I just use 'my', though 'our' is required when you use getopts() without a hash reference (mentioned in the module's perldoc page: perldoc -m Getopt::Std).


Advertisement