G'day anaconda_wly,

Here's a breakdown of what's happening with:

my $op = @_ ? ($seen{lc($field)}++ ? 'PUSH' : 'SET') : 'GET';

You've got a nested ternary operator here. Firstly, looking at the outer ternary. You've said you understand that when @_ is empty (i.e. FALSE), $op is assigned 'GET'. So, if @_ is not empty (i.e. TRUE), $op is assigned whatever ($seen{lc($field)}++ ? 'PUSH' : 'SET') evaluates to.

"($seen{lc($field)}++ ? 'PUSH' : 'SET')" is the nested ternary. If ($seen{lc($field)}++ is TRUE, 'PUSH' will be assigned to $op; if it's FALSE, then 'SET' will be assigned to $op.

"$seen{lc($field)}++" uses the postfix increment operator. This means that the value is returned first; afterwards the increment is performed. If the '++' was at the front (the prefix increment operator), the increment would be performed first and the incremented value would be returned. A simple code example might explain this better:

$ perl -Mstrict -Mwarnings -le ' my $x = 1; print $x++; print $x; my $y = 2; print ++$y; ' 1 2 3

$x starts as 1; $x++ evaluates to 1 for its print statement; afterwards, it's incremented to 2 as print $x shows. $y starts as 2; ++$y evaluates to 3 for its print statement, i.e. the increment is done first.

Back to "$seen{lc($field)}++". If %seen doesn't have a key lc($field), then $seen{lc($field)} will evaluate to FALSE (i.e. $op = 'PUSH'); after that evaluation, $seen{lc($field)} is incremented with the key being created (that's called autovivification) and the value being set at 1 (FALSE in numeric context is zero; 0 + 1 = 1). If %seen does have a key lc($field), its value will be at least 1 and $seen{lc($field)} will evaluate to TRUE (i.e. $op = 'SET'); $seen{lc($field)} will then be incremented again.

Using a hash called %seen (or similar name) and autovivifying keys (with a postfix increment) as they are seen is a common idiom. It would be worth your while studying this closely and ensuring you fully understand everything that's going on here: it's something you're likely to see often.

For wantarray, the context is the calling context; it's got nothing to do with the context inside the executing subroutine. The following may suffice to explain this:

my @return_values = function(@args); # wantarray TRUE my $return_value = function(@args); # wantarray FALSE

-- Ken


In reply to Re: What's this line means in HTTP::headers? by kcott
in thread What's this line means in HTTP::headers? by anaconda_wly

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.