
Perl secret operators and constants - fofoni
http://search.cpan.org/dist/perlsecret/lib/perlsecret.pod
======
bane
Perl is actually relatively easy to learn and get productive with. You can
write it in several transitional styles depending on where you are coming
from, and the community doesn't care all that much. It's a little bit like
using "Basic English" [1] to program with.

But it's a really deep language, and not necessarily in ways that academic
language designers want it to be. Perl always feels more like having a
conversation with your computer than programming it. These kinds of operators
are like having a whole host of additional highbrow verbs you can use in
conversation if you want.

1 -
[https://simple.wikipedia.org/wiki/Main_Page](https://simple.wikipedia.org/wiki/Main_Page)

------
BuildTheRobots
if nothing else, most of these operators have immense nicknames. Now I just
need an excuse to involve a flaming x-wing in my code...

    
    
        Operator     Nickname                     Function
        ======================================================
        0+           Venus                        numification
        @{[ ]}       Babycart                     list interpolation
        !!           Bang bang                    boolean conversion
        }{           Eskimo greeting              END block for one-liners
        ~~           Inchworm                     scalar
        ~-           Inchworm on a stick          high-precedence decrement
        -~           Inchworm on a stick          high-precedence increment
        -+-          Space station                high-precedence numification
        =( )=        Goatse                       scalar / list context
        =< >=~       Flaming X-Wing               match input, assign captures
        ~~<>         Kite                         a single line of input
        <<m=~m>> m ; Ornate double-bladed sword   multiline comment
        -=!   -=!!   Flathead                     conditional decrement
        +=!   +=!!   Phillips                     conditional increment
        x=!   x=!!   Pozidriv                     conditional reset to ''
        *=!   *=!!   Torx                         conditional reset to 0
        ,=>          Winking fat comma            non-stringifying fat comma
        ()x!!        Enterprise                   boolean list squash
        0+!!         Key to the truth             numeric boolean conversion
        ~~!!         Serpent of truth             numeric boolean conversion
        ||()         Abbott and Costello          remove false scalar from list
        //()         Leaning Abbott and Costello  remove undef from list

~~~
blue1
When I first learnt perl, twenty years ago, I loved it because it was full of
clever devices and syntactic virtuosism. Now, I don't like it anymore exactly
for the same reason. It's cool, but the wrong kind of cool.

~~~
kafkaesq
That's exactly the problem with Perl: cleverness -- especially _other people
's_ "cleverness" \-- gets to be tedious, if not an outright pain to put up
with and maintain, after a while.

~~~
BuildTheRobots
It's my own "cleverness" that winds me up the most. I've written a few bits of
5am code that amazingly and finally work though I no longer have any clue how
or why o_0

------
rwmj
I once managed to get some obfuscated Perl into an official Red Hat document
(as a subtle joke): [https://access.redhat.com/documentation/en-
US/Red_Hat_Enterp...](https://access.redhat.com/documentation/en-
US/Red_Hat_Enterprise_Linux/6/html/Virtualization_Administration_Guide/sect-
virt-win-reg-use.html) (search for "simple perl").

~~~
mcmillhj
I am not sure I would call this obfuscated. The only real confusing piece of
that code is if the reader doesn't know that you can use most punctuation
characters with s///g

------
mrbig4545
I write perl everyday, I never use this stuff.

~~~
xlm1717
I use a few of them in javascript. In fact, Venus can be shortened to just
"+", ie.

+"123234" +"1e29" +"abc" // -> NaN

~~~
dozzie
In Perl, no, because + is an unary identity operator, used e.g. to properly
leave function call parentheses:

    
    
      print +(1 + 2) * 3;
      print ((1 + 2) * 3);
      print (1 + 2) * 3;

------
ndonnellan
Reading this actually makes me feel like my company has done a good job of
outlawing the "write-only" type of perl.

Would this be valid?

$x +=!! =<>=~ s/readable-code//g;

~~~
philh
No, for the same reason that $x += = 3 is invalid.

------
chrisBob
Perl seems like a great language for a lot of text processing jobs, but while
its learning curve isn't too steep, it goes on forever. I will continue to use
it for quick little projects like cleaning up excel files, but I don't think I
could ever get into things like Perl golf.

~~~
vampirechicken
Perl golf is meant as a thought exercise. Code should be human readable. The
compiler will cope with verbose code just as easily as terse code.

------
ionforce
It's articles like this that give Perl a bad reputation.

~~~
vampirechicken
Mostly it was all the terrible code that got written in Perl by the first wave
of non-programmers to start building web apps.

~~~
anon4
So, same fate as PHP?

~~~
kbenson
Well, that's half PHP's problem. The other half is that it had very little
coherent design in the beginning, which resulted in idiosyncratic an confusing
syntax. It focused on a few things Perl did, while ignoring the important
context they were housed within (for one, the whole idea of _context_ as Perl
implements it, for another being consistent within your own rules).

Perl, as confusing as it can seem, has fairly consistent rules that it
follows. Learning those and keeping them in mind will generally let you intuit
how something is expected to work.

------
ceronman
The ones that I occasionally use:

 _0+ - venus - numification_ : When you really need a scalar value to be
represented as a number and not as a string. Useful when you're generating
JSON and you need the types to be correct.

 _@{[ ]} - baby cart - list string interpolation_ : This is very useful when
you need to interpolate arbitrary expressions inside a list. Useful when
you're generating an SQL query, for example:

    
    
      SELECT * FROM Office WHERE id IN (@{[ join(',', ('?') x @ids) ]})
    

_!! - bang bang - boolean conversion_ : When you want to return a boolean
value from a function, but you don't want to leak implementation details.

 _,= > \- winking fat comma - non-stringifying fat comma_ When you want to
create a hash whose keys are constants.

------
red_admiral
I wonder how many other languages have well-known secret operators?

I know of C's secret "limit" operator:

int x = 10; while (x --> 0) { printf("%i", x); }

prints 9876543210

Of course, in some ML dialects you can define your own opreators - such as (f
|> g) to mean (g o f) which is actually very useful.

~~~
byroot
That's not an operator, it's just precedence rules.

    
    
        while ((x--) > 0)

~~~
philh
None of these secret operators are actual operators.

(Some of them aren't even collections of operators - }{ is a form of code
injection, and the <<m=~m>> thing has only one operator, applied to a string
and a regex.)

------
stevebmark
Obviously you should never use any of these in a production code base. Perl
lets you write very bad, anti-team unreadable code in countless ways. You have
to actively avoid these traps designed as neat toys.

~~~
collyw
The second worst code I have had to fix was in Python (sadly the worst was in
Perl). Both bioinformaticians.

------
thoughtexpt
These do not look like "operators". They look like combinations of Perl
operators that produce a "useful" result. I mean they are built from the basic
operators.

------
tyingq
Interesting, but the title is misleading. They aren't operators, but rather
mostly combinations of operators with potentially useful side effects.

~~~
mcmillhj
Clearly you didn't read the DESCRIPTION section.

~~~
tyingq
A description section doesn't change the fact that the title is misleading.
Clickbait is clickbait.

------
tmaly
I have been using Perl since 1996, but I only use a handful of the secret
operators. For me, the power with Perl is CPAN and the stability of Perl 5.

~~~
squiguy7
If you are working on a code base with others, it would be cruel to use some
of these. However there are operators that could be beneficial to the advanced
Perl users.

~~~
collyw
When I use unusual or difficult language features, or basically anything I
expect someone else reading the code won't understand, I leave a comment.

------
pm24601
Ah, yes - Perl, the language I have learned many times.

------
excitom
My vote for most over-the-top: The Goatse.

------
jsonau
I used !! in many languages including Perl. Not exactly secret and very useful

