Search code examples
perlhashrefhash-reference

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"?


Solution

  • 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