It took me forever to figure it out, but here is what is happening with your code. The ternary operator ( - ? - : - ) is not being interpreted as you think it is. If you add parentheses, the behavior goes away:
#No longer "works"
sub ah{
my $p = my $htmlparagraphelement = shift;
(
wantarray ?
(my @givearr = print '<',$p,'>')
:
(my $fizix = '</'.$p.'>')
)
}
Instead, what you wrote is being interpreted as this:
#Same as the code without parentheses.
sub ah{
my $p = my $htmlparagraphelement = shift;
(
wantarray ?
(my @givearr = print '<',$p,'>')
:
(my $fizix )
) = '</'.$p.'>'
}
That's right, in Perl you can assign to a ternary operator expression! This "feature" allows you to assign to one of two different variables based on a conditional.
So, what is happening?
- wantarray is always true when your sub is called.
- Thus, you execute your array assignment and print the opening tag statement.
- Then your closing assignment is applied to the result of the ternary operator. This overwrites @givearr with the closing tag value that you intended to go into $fizix.
- Because you have no return statement, the result of the last statement executed is returned. This happens to be the closing tag.
The result: some truly bizarre behavior. It would make a great trick for some obfuscated code. Barring that, you should never, ever, ever (ever!) do it again.
I do intend to ++ your post, though, because this was really fun.
When's the last time you used duct tape on a duct? --Larry Wall
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.