Settings

Theme

Perlsecret - Perl secret operators and constants

search.cpan.org

59 points by yurifury 13 years ago · 18 comments

Reader

pyre 13 years ago

This 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.
  • btilly 13 years ago

    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:

        "Greetings " . get_planet() . "lings, we come in peace!"
    
    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.
    • mst 13 years ago

          "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 ${\}
    • pyre 13 years ago

      Depends on what you're doing. In a small string, there isn't much difference. It's more useful when doing other things:

        my $message = <<"HEREDOC";
        Greetings @{[ get_planet() ]}lings,
      
        We come in peace.
        
        Sincerely,
        Your New @{[ get_invaders() ]} Overlords
        HEREDOC
      
      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.
      • btilly 13 years ago

        You 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.)

JadeNB 13 years ago

Peteris Krumins wrote about some of these operators some years ago: http://www.catonmat.net/blog/secret-perl-operators.

nemo1618 13 years ago

As someone who loves obfuscated Perl...thanks! These are some great tricks.

  • hippich 13 years ago

    From someone who loves Perl, please do not use it in production :)

    Although, it is really interesting find!

    • prakashk 13 years ago

      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.)

    • irahul 13 years ago

      > 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.

    • hahainternet 13 years ago

      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.

      • to3m 13 years ago

        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.

snorkel 13 years ago

Part of the reason I've stopped writing Perl apps, the operator noise is getting out of hand.

  • hahainternet 13 years ago

    Could you give examples? All of the recent Perl improvements have introduced operators that seem extremely useful to me and reduce noise.

  • kamaal 13 years ago

    Proof please.

    Don't get me wrong, but comment that start out like this generally turn out to be trolls.

  • duskwuff 13 years ago

    None of these are new operators. They're just creative ways of abusing existing operators, most of which aren't even unique to Perl.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection