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.


In reply to Re^7: Tidy up conditions by martin
in thread Tidy up conditions by tel2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.