in reply to Getting Getopt::Long values into a hash

Are you looking for this?

$getoptret = $p->getoptions( \%my_hash, 'house', 'dog|d:s' );

If you want more control over where things are stored you could also do something like:

$getoptret = $p->getoptions( 'house' => \$my_hash{house}, 'dog|d:s' => \$my_hash{mutt}, );

Edit: D'oh, too slow

Good Day,
    Dean

Replies are listed 'Best First'.
Re^2: Getting Getopt::Long values into a hash
by jeanluca (Deacon) on Nov 08, 2005 at 14:19 UTC
    It only seems to work when I use it like:
    $getoptret = $p->getoptions( 'house' => \$my_hash->{house}, 'dog|d:s' => \$my_hash->{mutt}, );
    what is exactly the difference between $my_hash->{something} and $my_hash{something} ?

    Thanks a lot!
    Luca

      It seems that you must have $my_hash as a hashref, rather than having a %my_hash. The arrow-form adds a dereference to the LHS value. In my example, #1 and #2 are equivalent:

      my $ref = { foo => "bar" }; print ${$ref}{"foo"}; #1 prints "bar" print $ref->{"foo"}; #2 prints "bar" print $ref{"foo"}; #3 prints "" # and under strictures throws a: # "Global symbol "%ref" requires explicit package name"

      $ref{"foo"} only returns a value if there is a hash named %ref.

        I almost start my script with: my $my_hash = {} ; As far as I know this should create a hash reference ?!
        What exactly means 'dereference to the LHS value' ?
        When it comes to variables and references I often get confused!

        Thnx
        Luca