Wednesday, July 4, 2012

What is "-f>@+?*<.-&'_:$#/%!" in Perl?

Back in 2009, someone posted a question on Stackoverflow asking:

How do you google for "-f>@+?*<.-&'_:$#/%!"?

The poster was actually trying to figure out what -f>@+?*<.-&'_:$#/%! did in Perl, but before I knew it, the question had been migrated to SuperUser as a question about how to use Google. It has been moved again, and now it lives on WebApps.

I felt like I did a good job going through the expression the first time, and I really do not want my explanation to be lost in the StackExchange maze. So, here it is:

Writing -f>@+?*<.-&'_:$#/%! out piece by piece, we have:

  • -f: perldoc -f -X. If no file name is specified (as is the case here) checks if $_ contains the name of a plain file

  • >: Checks if the RHS LHS is greater than the LHS RHS (thanks MartinS for noticing the error)

  • @+: In scalar context, returns the number of elements in @+

  • ?: the conditional operator

  • *<: The glob for main::<

  • .: String concatenation operator

  • -&'_: Invokes a function main::_. FYI, ' is the Perl4 style package name separator. Try perl -MHTML'Template -e 1.

  • : : Continuing with conditional operator

  • $# : The output format for printed numbers

  • / : Division operator

  • %! : %ERRNO; see perldoc -v '%!'

PS: See also notes on how to write maintainable Perl: Basically, if you think Perl is -f>@+?*<.-&'_:$#/%!, and you aspire to write stuff like that in production code, then, yeah, you'll have problems.

PPS: I also wrote a blog post on writing readable and maintainable Perl for O'Reilly.

4 comments:

  1. $# is no longer supported

    ReplyDelete
    Replies
    1. Wonderful! Someone actually read the post. @Anon I guess I forgot to mention that $# no longer exists because it was in the comments of my original post. Chas. Owens' commit noting the status of $# in Perl documentation, the P5P thread where this was all discussed, the commit where $# was removed, and the documentation for variable $#:

      =item $OFMT
      =item $#

      The output format for printed numbers. This variable is a half-hearted attempt to emulate awk's OFMT variable. There are times, however, when B and Perl have differing notions of what is in fact numeric. The initial value is %.I<n>g, where I<n> is the value of the macro DBL_DIG from your system's float.h. This is different from awk's default OFMT setting of %.6g, so you need to set $# explicitly to get awk's value. (Mnemonic: # is the number sign.)

      Use of $# is deprecated.

      Delete
  2. > >: Checks if the RHS is greater than the LHS

    Errm... I thought < checked if RHS is greater then LHS.


    --
    MartinS

    ReplyDelete