Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
return 1 if ( $d->{foo} && $d->{foo} =~ /X/i )

Most of the following has already been covered by trippledubs, but let me lay it out in order:

  1. Something if Something is a statement modifier. That line of code is the same as

    if ( $d->{foo} && $d->{foo} =~ /X/i ) { return 1 }

    so let's look at it in that order. (Statement modifiers can make Perl read a lot more like English.)

  2. The first part of the if condition is $d->{foo}. ->{...} dereferences a hash reference (see perlreftut), in other words, $d is a reference to a hash, and we're asking Perl to look up the key foo in that hash.

  3. Since in the first part of the condition we're not applying any other operators other than &&, this means we are checking for whether $d->{foo} holds a true or a false value. Why would we want to do this? Most likely, because if $d->{foo} was undef, Perl would issue a warning later on when we use the =~ operator (more on that below). This also applies if the hash did not even contain a key foo - in this case Perl would also return undef for that key. (Note: You can check for undef explicitly with the defined function, and check for the existence of a hash key with exists. The fact that this code does not use these means that if $d->{foo} were to hold the value "0", then that would still be evaluated as "false". But since this code is looking for "X", that should not be a problem in this case.)

  4. The && operator "short-circuits", meaning that if the left side is false, the right side does not even get executed and tested, because if the left is false, we already know that the whole && will be false. If the left side is true, then the operator will test the right side.

  5. In the right side of the condition, $d->{foo} =~ /X/i, the =~ operator basically applies the regular expression /X/i to the string value of $d->{foo}.

  6. The regular expression (see perlretut) /X/i basically means "find X case-insensitively" - the /i is a "modifier" on the regular expression /X/ that turns on case-insensitive matching.

  7. The return statement will return the value 1 from the subroutine (or similar block) it is inside of, aborting its execution at that point (see also perlsub).

So taking all of that together, that line of code reads as "return 1 if the hash referenced by $d contains a key foo with a true value that also contains the string X (case-insensitively)".

Update 2019-08-17: Updated the link to "Truth and Falsehood".

In reply to Re: Simple Q. for You by haukex
in thread Simple Q. for You by Dan-Y

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (5)
    As of 2021-01-24 16:52 GMT
    Find Nodes?
      Voting Booth?