zachary adams
zachary adams

Reputation: 83

Accessing args in Perl Hash Ref

$self->doSomething({
    record     => $record,
    listing    => [ $foo, $bar, $baz ],
    passedargs => { something => $val, another => $val2 }
});

accessing args within a hashref.

So if I wanted to access record I would

my $record = $args->{record};

If I wanted to access listing I would

my @listing = $args->{listing}; 

How would I access Passedargs? If I just wanted to access "something" or "another"?

Upvotes: 1

Views: 82

Answers (2)

Miller
Miller

Reputation: 35208

If you want assign the values in listing to an array, please note that you'll need to dereference it:

my @listing = @{ $args->{listing} };

To access the fields in passedargs, you simply need to use the following syntax:

my $something = $args->{passedargs}{something};

For more details, take a look at: perldsc - Perl Data Structures Cookbook

Upvotes: 3

Andy Lester
Andy Lester

Reputation: 93755

$args->{passedargs} is a hashref itself, so you would do:

my $something = $args->{passedargs}->{something};

Upvotes: 1

Related Questions