Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Pushing hash ref onto array ref

by Fletch (Bishop)
on Apr 12, 2021 at 05:08 UTC ( [id://11131120]=note: print w/replies, xml ) Need Help??


in reply to Re: Pushing hash ref onto array ref
in thread Pushing hash ref onto array ref

I'd use push @{ $hash->{'key'} }, $new out of habit personally, but if you've got a new enough perl (post 5.24.0 according to perlref; you can use back to 5.20 with a feature declaration) you could use the new postfix deref syntax push $hash->{'key'}->@*, $new instead.

Edit: Clarified version requirement.

The cake is a lie.
The cake is a lie.
The cake is a lie.

Replies are listed 'Best First'.
Re^3: Pushing hash ref onto array ref
by kcott (Archbishop) on Apr 12, 2021 at 10:58 UTC

    I've been making a point of using "perlref: Postfix Dereference Syntax". It removes the need for nested braces and brackets (e.g. %{$aref->[$i]} and @{$href->{$key}}) which can be confusing. It also reads linearly; i.e. ref->deref_to (e.g. $ref->%*) vs. deref_to{ref}.

    For very simple dereferencing, I often find myself using the older (original) syntax (e.g. $$scalarref). This is no doubt out of years of habit and muscle memory. I still think $scalarref->$* is clearer and I try to remember to use that; it also adds consistency to my code.

    I also find "perlref: Postfix Reference Slicing" to be a lot clearer; especially when the initial reference is a complex variable itself.

    Of course, that all requires a sufficiently new version of Perl. For instance, I can't use that for $work where I'm constrained to 5.16; for personal code, where I'm using the latest Perl (currently 5.32) that's not an issue.

    — Ken

      Of course, that all requires a sufficiently new version of Perl. For instance, I can't use that for $work where I'm constrained to 5.16; for personal code, where I'm using the latest Perl (currently 5.32) that's not an issue.

      It didn't occur to me that they both worked because I am using an old version of Perl v5.16.3 on the server. Because I hadn't made the connection with versioning, I didn't think to try it locally on v5.32.1...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11131120]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (8)
As of 2024-04-24 10:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found