Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^3: Arrow Operator Question

by hv (Prior)
on Mar 27, 2023 at 17:28 UTC ( [id://11151266]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Arrow Operator Question
in thread Arrow Operator Question

my $f = $h->{b}{c}{d} if exists($h->{b}{c}) and exists($h->{b}{c}{d});

This is dangerous: the nature of my $var = ... if ... is predictable but very noninuitive (it acts something like a state variable). It has long been regarded as a bug that perl core would dearly love to fix, but cannot due to back-compat.

I recommend always either splitting out the declaration from the assignment:

my $f; $f = $h->{b}{c}{d} if exists($h->{b}{c}) and exists($h->{b}{c}{d});

.. or using state explicitly when that's what you actually intend:

use feature 'state'; # 'use 5.10' or later also gives this automatica +lly state $f = $h->{b}{c}{d} if exists($h->{b}{c}) and exists($h->{b}{c}{d +});

Replies are listed 'Best First'.
Re^4: Arrow Operator Question
by BillKSmith (Monsignor) on Mar 28, 2023 at 18:07 UTC
    I was a bit surprised that my my did work as I intended. I choose to use it in the interest of conforming to the original code as much as possible. Thanks for the explanation.
    Bill

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-23 11:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found