Perlsecret - Perl secret operators and constants
search.cpan.orgThis fails to mention the most useful part of the "baby cart":
"Greetings @{[ get_planet() ]}lings, we come in peace!"
Evaluating functions directly inside of a string. It's sort of alluded to with "@{[ keys %hash ]}", but in doesn't come right out and say it.You get the wrong context when you do that. Try it with, say, localtime and see what happens. String interpolation is more obvious, and only one character more in normal formatting, one character less in golf:
As it says, the whole point of the baby cart is to force list interpolation. If you don't specifically want that, don't do it."Greetings " . get_planet() . "lings, we come in peace!"
will give you scalar context as well. My http://shadow.cat/blog/matt-s-trout/madness-with-methods write-up covers some other uses of ${\}"Greetings ${\ get_planet() }lings, we come in peace!"Depends on what you're doing. In a small string, there isn't much difference. It's more useful when doing other things:
Sure you can jam them into variables first, or use sprintf, but it's a matter of preference so long as you know what you're doing.my $message = <<"HEREDOC"; Greetings @{[ get_planet() ]}lings, We come in peace. Sincerely, Your New @{[ get_invaders() ]} Overlords HEREDOCYou can also use http://search.cpan.org/dist/Interpolation/lib/Interpolation.... for this problem. But if you've got a lot of text, I prefer to bite the bullet and graduate to a proper templating system.
However my point still stands. You need to be aware that it does not simply interpolate the function output in, it also switches you from scalar to list context. (IMO context is the worst idea in Perl, but it is here to stay so you need to know about it.)
Peteris Krumins wrote about some of these operators some years ago: http://www.catonmat.net/blog/secret-perl-operators.
As someone who loves obfuscated Perl...thanks! These are some great tricks.
From someone who loves Perl, please do not use it in production :)
Although, it is really interesting find!
While I generally agree that the discretion must be observed when working on production code, I don't think the author is advocating these to be of wide use.
These are very useful when you play Perl Golf, and can be pretty powerful in the hands of an expert player (sadly, my dreams of becoming one has remained just that.)
The author of this document was a regular in Perl Golf tournaments and also in the Fun With Perl mailing list, which was a precursor and launchpad for several of these tournaments.
For those interested, here is a series of articles about "The Lighter Side of Perl Culture": http://www.perlmonks.org/?node_id=410774
See also, "Perlgolf History" at http://terje2.frox25.no-ip.org/perlgolf_history_070109.pdf (not sure if this link is still alive.)
Whoa!!!!, thanks for that pdf. Who ever compiled it has just done an awesome job!
If I remember correctly, that would be "mtve" [1] (who sometimes went by a slightly longer "mtveurope"). If I had to pick the top three all-time Perl golfers, he would be one of them.
> From someone who loves Perl, please do not use it in production :)
From another someone who has written fair amount of perl, some of them are simple idioms suitable for production use; rest are just an exercise in curiosity.
The venus operator "0+ $input"(or `0 + $input` as I will write it) is an acceptable and recommended way to convert input to number(i.e if you know your your input is a valid number in string form). !! isn't unique to perl - I have used it in JS, C etc to convert a value to boolean. @{[ ]} is useful for logging, thought it does look difficult to grok in first look. Goatse is basically list assignment and then using it in scalar context; I won't do that but assigning to list is frequently done `my ($a, $b) = $input =~ /regex/`. =<>~ is just badly formatted `my ($a, $b) = <> =~ /regex/` will read fine to a perl programmer.
Very few of these are really unsuitable for production. High preference and conditional operation operators are probably the ones I would be most shocked to see.
The rest (especially goatse, venus and things like X-Wing) should be fairly intuitive from operator precedence.
Part of the problem, if you want to call it that, is probably that people like to put spaces in their code. This leads them to see magic, when in fact they're just insufficiently practiced at scanning the code as the computer sees it.
Part of the reason I've stopped writing Perl apps, the operator noise is getting out of hand.
Could you give examples? All of the recent Perl improvements have introduced operators that seem extremely useful to me and reduce noise.
Proof please.
Don't get me wrong, but comment that start out like this generally turn out to be trolls.
None of these are new operators. They're just creative ways of abusing existing operators, most of which aren't even unique to Perl.