Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

perl problem using "getopts"

  • 05-02-2007 06:28PM
    #1
    Registered Users, Registered Users 2 Posts: 4,349 ✭✭✭


    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, Registered Users 2 Posts: 6,652 ✭✭✭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, Registered Users 2 Posts: 4,349 ✭✭✭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, Registered Users 2 Posts: 6,652 ✭✭✭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