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

Re^5: Tidy up conditions

by martin (Friar)
on Mar 20, 2015 at 03:43 UTC ( [id://1120692]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Tidy up conditions
in thread Tidy up conditions

Actually, I did mean values and not hash keys. Here is an example of how boolean false values would defeat our intent to short-cut at the first match:
my %access = ('foo' => 0, 'bar' => 123); print $access{'foo'} || $access{'bar'}; # prints 123
The key 'foo' exists but the zero score makes perl evaluate the second operand, too. With perl version 5.10.0 and up, the defined-or operator can help, as it short-cuts only on undef:
my %access = ('foo' => 0, 'bar' => 123); print $access{'foo'} // $access{'bar'}; # prints 0
Older perls don't have this operator, however.

Replies are listed 'Best First'.
Re^6: Tidy up conditions
by tel2 (Pilgrim) on Mar 21, 2015 at 01:35 UTC
    Hi again Martin,

    I've incorporated the // in my code like this:

    $item = 'Test'; $level = $access{$item} // ($item =~ /^([^:]+):/ && $access{"$1:*"}) // $access{'*'} // 999; print "level='$level'\n";
    Unfortunately, when the regex doesn't match, it seems to short-circuit, and this is printed:
    level=''

    Any ideas why? I assume it's something to do with the line which contains the regex not being 'defined', but I'm not sure.

    Meanwhile, I've worked around it by putting the regex first, like this:

    $access{'*'} = 30; $item = 'Test'; $item =~ /^([^:]+):/; $level = $access{$item} // $access{"$1:*"}) // $access{'*'} // 999; print "level='$level'\n";
    And I think that's working (i.e. it prints "level='30'" in the above case).
    If you've got a better adjustment to this code, which doesn't involve using that regex unless needed, I'd love to see it.  Otherwise, no worries - I'm pretty happy.

    Thanks again.

      Because $item =~ /^([^:]+):/ gives a false value that is not an undefined value when it fails (and $false && $blah returns $false not undef and $notundef // $blah returns $notundef not $blah).

      - tye        

        Well explained, tye.

        Thanks very much for that.

        Tel2

      You stated in your initial post that $item contained a string in the format 'X:Y'. If you were right, that match would of course not fail.

      However, if that precondition is dropped and the match is allowed to fail, your new code will put an undefined value $1 into a string when that failure happens. Undef stringifies to an empty string and all may seem well, but your future maintainer might wonder if you did this on purpose or just overlooked an edge case.

      How could the match fail? It fails if the first character is a colon or there is no colon at all. You could modify the match so that it still matches part X of an 'X:Y' string but never fails, like this:

      $item =~ /^([^:]*)/; $level = $access{$item} // $access{"$1:*"}) // $access{'*'} // 999;

      The match will now capture the whole string if there is no colon, and an empty string if the first character is a colon. If you want to assign the "Test:*" level to item "Test" you are done.

      If you rather want to assign the "*" level to item "Test" this can also be made foolproof. You could match with colon and look at the success of the match:

      my $partial = $item =~ /^([^:]+):/ ? "$1:*" : '*'; $level = $access{$item} // $access{$partial}) // $access{'*'} // 999;

      And so on. I am not dogmatic about turning on warnings but consider it good style not to ignore what is defined and what isn't.

        Thanks for that, martin. Just noticed your post recently.
        Thanks Anonymous Monk,

        Unfortunately, the output of:
           perl -MO=Deparse,-p test.pl
        doesn't tell me much (in this case):

        ($item = 'Test'); ($level = ((($access{$item} // (($item =~ /^([^:]+):/) && $access{"$1: +*"})) // $access{'*'}) // 999)); print("level='${level}'\n"); test.pl syntax OK

        But thanks for the link to the other things on the debugging page.

Re^6: Tidy up conditions
by tel2 (Pilgrim) on Mar 20, 2015 at 05:23 UTC
    Well said, Martin!

    Good thing I'm using v5.10.1.

    I'd never seen the // operator.
    I'm so glad I asked...and you answered.

    Tel2
    (Another satisified PerlMonks shopper)
    PS: Give yourself a pay rise, could ya?

Log In?
Username:
Password:

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

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

    No recent polls found