Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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?

  1. wantarray is always true when your sub is called.
  2. Thus, you execute your array assignment and print the opening tag statement.
  3. 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.
  4. 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

In reply to Re: new way to code html? by ColonelPanic
in thread new way to code html? by Don Coyote

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 taking refuge in the Monastery: (2)
    As of 2018-05-23 00:24 GMT
    Find Nodes?
      Voting Booth?