Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: scope of variables in a sub

by GrandFather (Saint)
on Sep 24, 2020 at 00:39 UTC ( [id://11122142]=note: print w/replies, xml ) Need Help??


in reply to Re: scope of variables in a sub
in thread scope of variables in a sub

Well spotted! Assignment in an if condition is just too smelly for my brain to process.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re^3: scope of variables in a sub
by tobyink (Canon) on Sep 24, 2020 at 10:29 UTC

    Assignment in an if is fine if you're doing:

    if ( my $var = something() ) { # use $var here }

    (Ditto while.)

    But if $var has a wider scope, it's probably a bad idea.

      Even then there are two major issues to my mind:

      1. The clutter around the assignment hides the assignment making it harder to see where the variable gets its value
      2. It is easy to confuse = and == so using the assignment operator in a conditional expression makes code harder to maintain

      Point 2 is seen as sufficient of an issue that many compilers generate warnings for assignment in conditional expressions. I love the way that Perl allows terse functional chords that allow the equivalent of many lines of code in other languages to be wrapped up in a single compact statement. But playing tricks with side effects just to save a line of code doesn't sit well with me.

      I'm completely fine with the equivalent assignment in a while statement though - go figure.

      Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re^3: scope of variables in a sub
by perlfan (Vicar) on Sep 24, 2020 at 02:56 UTC
    >Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

    Yup. OP must be doing just this. xD

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-25 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found