Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Hidden features of Perl

by vxp (Pilgrim)
on Jun 22, 2009 at 00:48 UTC ( #773443=perlmeditation: print w/replies, xml ) Need Help??

I stumbled upon this list of "hidden features of perl" and thought it'd a good idea if I got some of your replies as well!

http://stackoverflow.com/questions/161872/hidden-features-of-perl

Just as on the site, here a few "requirements":

Try to limit answers the Perl core and not CPAN

Please give an example and a short description

Replies are listed 'Best First'.
Re: Hidden features of Perl (More Secret Operator References)
by eyepopslikeamosquito (Bishop) on Jun 22, 2009 at 23:44 UTC
      I believe that @{[]} is known as the "baby buggy". Can't remember where I read that though.

        Originally, a crude name was proposed for this operator, as indicated by this response from BooK to Sebastien Aperghis-Tramoni:

        > Hey Philippe, why don't you give the name we found for @{[]} ?

        Because *you* found it, and want to have *my* name associated with it.

        To deflect attention from the taboo name, BooK started another fwp thread, suggesting "Baby Cart" ... which was objected to by native English speakers on the grounds they'd never heard it used in everyday speech. Suggested alternatives included "Baby Carriage" and "Pram". Though many other names have been proposed, including:

        it seems that BooK's quirky Baby Cart has won this fascinating naming war.

        BooK References

        Quoting BooK from Interview with Philippe Bruhat (August 2015):

        I've been hanging around on the Fun with Perl mailing for a long time, and this is where Josť Castro started to ask for the names of the most famous secret operators for his OGSOP (Obfuscation, Golfing and Secret Operators in Perl) book project. I came up with the name "baby cart" for @{[]}, and I loved that name so much that I wanted to make sure it would be the one that everyone would use. The only way to do that was to write the reference manual myself...

        Early Historical References

        Comparing Baby Cart with String Interpolation in Other Languages (Golf)

        Though usually too long for golf, Baby Cart occasionally features in competitive golf solutions, as you will discover by searching for Baby Cart in the following nodes:

        Based on these nodes, consider how to create a "Dear John" string in each of the four languages:

        "Dear $name" # Perl and PHP "Dear %s" % expr # Python and Ruby % printf-like operator "Dear {0}".format(expr) # Python format string method "Dear "+`expr` # Python backticks (TMTOWTDI) "Dear #{expr}" # Ruby string interpolation "Dear @{[expr]}" # Perl "Baby Cart" string interpolation "Dear {expr}" # Perl 6 version of Baby Cart (I think)

        My 102 stroke Perl solution to the Saving Time challenge used Baby Cart to create a printf format string on the fly:

        printf"%@{[.1*vec'XXXXXXXXXXXX',$_,8]}s",($_^$`%12?g:p)&($_^$'/5?g:u)| +"H "for map{$_,11-$_}<>!~/:/..5

        What's the shortest way to create this peculiar printf format string from an expression (expr) in each of the four languages?

        TechniquePerlRubyPythonPHP
        String interpolate"%@{[expr]}s""%#{expr}s""%%%ds"%expr~X.expr.s
        Number of strokes1311129

        where X above is the character with ord value 218. Poor old Baby Cart baby-stepped to the finish line in last place in this oddball race. This is hardly surprising given Baby Cart was not really designed; it was independently "invented" shortly after Perl 5 was released in 1994 by both L.Wall and R.Schwartz ... and later celebrated as one of Perl's secret operators.

        Updated: Many changes were made and extra references added long after the original response was made.

      Tadpole secret operator ~~($$..!$$) can be used as a kind of counter
      print $_ , ' => ', ~~($$..!$$), "\n" for qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
Re: Hidden features of Perl
by Arunbear (Prior) on Jun 22, 2009 at 10:12 UTC
Re: Hidden features of Perl
by Jenda (Abbot) on Jun 22, 2009 at 13:24 UTC

    Esoteric? What the heck is esoteric about them? Agrrr. Anything that's not apparent to someone with strictly imperative background with no computer science knowledge whatsoever is deemed "esoteric". Nonsense.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      W(hat|TF) does this have to do with "imperative" or "computer science?" It seems non-obvious to me that a magic filehandle called "DATA" reads everything in the current file after a line containing only "__DATA__". Perl has a lot of non-obvious features, and that thread lists quite a few of them.

        It seems that you are arguing semantics, which isn't the point of this post.. post whatever _you_, personally, consider a "hidden feature". Don't worry about what others consider esoteric. :)

        Most of the stuff is something that looks esoteric to someone who was taught C or Fortran or who taught him/her-self VB and is so used to imperative programming, that he/she cannot think outside that old tiny box. The people that find references hard and higher order functions rocket science.

        And the DATA filehandle is no more esoteric than a print statement. A bit unusual maybe, non-obvious probably, but esoteric? That's a very big stretch of the meaning of the word.

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

Re: Hidden features of Perl
by Anonymous Monk on Jun 22, 2009 at 02:28 UTC
    Hidden? I don't think there are any left hiding :)
Re: Hidden features of Perl
by whakka (Hermit) on Jun 22, 2009 at 23:23 UTC
    Not so much a "hidden feature" but a shout-out to Inline::C for making the task of hooking C code to Perl criminally easy. I would include all examples in the Cookbook to be good examples of "hidden" features, particularly the prospect of object-oriented C with Perl5 API calls.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (1)
As of 2022-11-27 12:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?