7 ms·
Perl secret operators and constants
- ndonnellan 11y agoReading 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;
- chrisBob 11y agoPerl 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 11y agoPerl 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.
- mrbig4545 11y agoI write perl everyday, I never use this stuff.
- yati 11y agoSame here, I didn't even know about most of these, except the list interpolation trick, "@{[ fncall() ]}", which is pretty common where I work.
- xlm1717 11y agoI use a few of them in javascript. In fact, Venus can be shortened to just "+", ie. +"123234" +"1e29" +"abc" // -> NaN
- dozzie 11y agoIn 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;
- dozzie 11y agoMe too. Except maybe for goatsee operator sometimes, because it gives you a forced list context, similar to "scalar" builtin.
- jerf 11y agoNumification is important if you output to another language, for instance, with JSON, where the other language will care about a string showing up where a number should have been. List interpolation is occasionally necessary for syntax reasons, though I don't think of that as an operator, but the composition of its parts. !! is for if you don't really love the loosey-goosey nature of Perl, and want to have a function that really, truly returns a boolean, and doesn't just return something that may or may not be a value that is either falsy or not. If you think in OO you may not want people coming to depend on the exact falsy value returned, which may expose internals. Generally I just go with the general Perl/scripting language approach, except when I know I'm leaking internals that I really, really don't want people to couple to. The rest are mostly for show.
- rwmj 11y agoI 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_Enterprise_Linux/6/html/Virtualization_Administration_Guide/sect-virt-win-reg-use.html https://access.redhat.com/documentation/en-US/Red_Hat_Enterp... (search for "simple perl").
- mcmillhj 11y agoI 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
- ionforce 11y agoIt's articles like this that give Perl a bad reputation.
- vampirechicken 11y agoMostly it was all the terrible code that got written in Perl by the first wave of non-programmers to start building web apps.
- beefhash 11y agoThen the bad reputation of PHP was the second wave of non-programmers building web apps?
- anon4 11y agoSo, same fate as PHP?
- kbenson 11y agoWell, 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.
- logicallee 11y agoit's the existence of this that gives Perl a bad reputation. You can write Perl as though it were Python. In the Perl world that's called "baby talk" https://www.google.com/search?q=perl+"baby+talk" https://www.google.com/search?q=perl+"baby+talk" in the real world it's called clean, maintainable code.
- justinator 11y ago
- BuildTheRobots 11y agoif 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 11y agoWhen 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 11y agoThat'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.
- red_admiral 11y agoI 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 11y agoThat's not an operator, it's just precedence rules. while ((x--) > 0)
- philh 11y agoNone 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.)
- excitom 11y agoMy vote for most over-the-top: The Goatse.
- tyingq 11y agoInteresting, but the title is misleading. They aren't operators, but rather mostly combinations of operators with potentially useful side effects.
- jsonau 11y agoI used !! in many languages including Perl. Not exactly secret and very useful
- bane 11y agoPerl 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
- tmaly 11y agoI 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 11y agoIf 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 11y agoWhen I use unusual or difficult language features, or basically anything I expect someone else reading the code won't understand, I leave a comment.
- thoughtexpt 11y agoThese 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.
- pm24601 11y agoAh, yes - Perl, the language I have learned many times.
- stevebmark 11y agoObviously 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 11y agoThe second worst code I have had to fix was in Python (sadly the worst was in Perl). Both bioinformaticians.
- ceronman 11y agoThe 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.