Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
(Replying to the Update in the OP.)
The when ([wxID_YES]) {....} also works (why?)

I also wondered that. :-) Here is the answer I came up with:

  1. From Experimental Details on given and when, it is clear that this is not one of the “10 exceptional cases” in which the expression is treated as a boolean. Therefore, smartmatching applies.

  2. From Smartmatch Operator:

    The smartmatch implicitly dereferences any non-blessed hash or array r +eference, so the HASH and ARRAY entries apply in those cases.
  3. Also from Smartmatch Operator:

    Right operand is an ARRAY: ... Any ARRAY smartmatch each ARRAY element like: grep { Any ~~ $_ } ARRAY
  4. and

    Num nummy numeric equality like: Num == nummy

    where nummy is defined as

    Either an actual number, or a string that looks like one.


when ([wxID_YES])

creates an anonymous array reference, and populates the array with the result of calling wxID_YES() — namely, 5103 — then it dereferences the array reference (1) & (2), and performs a smartmatch on the elements (3), effectively:

grep { $selection ~~ $_ } (5103)

which (4) reduces to:

$selection == 5103

which actually succeeds in being DWIM.

All clear now?   ;-)

*   *   *

Also interesting is tobyink’s observation about constant, because

use constant FOO => 42;

is implemented as a subroutine named FOO. The significant point is that this subroutine is prototyped to take no arguments:

sub FOO() { return 42; }

which (together with some other requirements) allows Perl to inline it — that is, wherever FOO appears in the code (outside of quotes), it is replaced with the value 42, so smartmatching just works as expected. See Constant Functions.

Athanasius <°(((><contra mundum

In reply to Re: converting from switch to given-when by Athanasius
in thread converting from switch to given-when by jmlynesjr

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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (7)
    As of 2018-02-25 18:12 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (314 votes). Check out past polls.