Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2024-03-19 06:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found