SPUG: Wacky variable parsing rules attack again!

Tim Maher tim at consultix-inc.com
Wed Aug 25 17:15:42 CDT 2004


I just got bit by an old bugaboo that catches up with me periodically.

The problem is that Perl runs normally in Obfuscation Mode (aka Poetry
Mode), and sooner or later your poetic license is likely to be suspended,
and you'll get stranded.

An explanation of a the problem, from my upcoming book, follows below.
Enjoy 8-}

*--------------------------------------------------------------------------*
| Tim Maher, CEO     (206) 781-UNIX      (866) DOC-PERL     (866) DOC-UNIX |
| tim(AT)Consultix-Inc.Com  http://TeachMePerl.Com  http://TeachMeUnix.Com |
*+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*
| Watch for my upcoming book: "Minimal Perl for Shell Users & Programmers" |
|  Classes:  UNIX Fundamentals for "Power Users", October 4-7, SeaTac WA   |
*--------------------------------------------------------------------------*

As the first programmer to attempt the creation of a full-fledged source
code beautifier for Perl (see http://teachmeperl.com/perl_beautifier.html ),
I can assure you that some of Perl's ideas about how it should parse its
programs are unconventional, surprising, and potentially troublesome.

As a case in point, here's the usage string from a program that 
computes compound interest -- the user supplies the numeric investment
amount and interest rate on the command-line as switch variables, as
indicated by symbols in the message:

"Usage: $0 -amount=$  -rate=%"

is parsed as

"Usage: $0 -amount=$-  rate=%"

and prints as

"Usage: $0 -amount=0  rate=%"

if the (rarely used) built-in variable $- is set to zero (as usual). I know
of no other programming language that feels it's sensible to skip over
spaces in order to assemble variable names, but Perl prides itself in doing
stuff like this -- so watch out!  The fix for the problem shown above
is of course to backslash the literally intended $ symbol.

*--------------------------------------------------------------------------*
| Tim Maher, CEO     (206) 781-UNIX      (866) DOC-PERL     (866) DOC-UNIX |
| tim(AT)Consultix-Inc.Com  http://TeachMePerl.Com  http://TeachMeUnix.Com |
*+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*
| Watch for my upcoming book: "Minimal Perl for Shell Users & Programmers" |
|  Classes:  UNIX Fundamentals for "Power Users", October 4-7, SeaTac WA   |
*--------------------------------------------------------------------------*


More information about the spug-list mailing list