Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: new way to code html?

by ColonelPanic (Friar)
on Nov 21, 2012 at 14:29 UTC ( [id://1004929]=note: print w/replies, xml ) Need Help??


in reply to new way to code html?

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

Replies are listed 'Best First'.
Re^2: new way to code html?
by Don Coyote (Hermit) on Nov 21, 2012 at 20:48 UTC

    Colonel, way to go! :)

    yah, once again precedence rears its ugly head and cuts us down at the knees.

    I lumped in wantarray with the ternary operator, to get what I thought was the wantarray operation... not so though hey. So that was what was happening. Glad you had fun working that out, I had fun constructing it.

    Pretty happy with the side effect heaven access level 1 granted card too ;)

    and yes i made scribe, next level MONK!!!!!

    votes all ++


    foreach(@{$Coyote->{thread}{users}}){wantvote ? ++ : -- } = ++ #whats happening here CP??? lol

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1004929]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 04:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found