Chapter 9. Expand Your Perl Foo

Hacks 84-101

What exactly is a Perl guru? Is it someone who's programmed Perl for years and years? Is it someone with a dozen modules on the CPAN? Is it someone with patches in the core or her name in the Preface.[1]

Perhaps instead a guru is someone who knows something that most people never knew existed. A real guru can apply that knowledge productively and appropriately to solve a difficult problem with apparent ease.

Want to be a Perl guru? Here's some of the magic you may never have suspected. Absorb these secrets. Recognize the situations where you can apply them. Then you too will be a guru.

Hack #84. Double Your Data with Dualvars

Store twice the information in a single scalar.

Some languages are really picky about the contents of a variable. If the variable is a string, it's always a string. If the variable is a number, it's always a number—especially of a certain type and size.

Perl's not that picky; it happily converts back and forth depending on what you do with the variable. Consequently, one variable may hold several different pices of data. You can even peek inside Perl's storage and do different things depending on how you treat your variable—returning entirely different values whether you treat it as a number or a string.

The Hack

Consider a program that has a lot of constants—say, a graphical program [Hack #16] with screen size, color depth, difficulty level, and so on. If you're debugging such a problem, it can be difficult ...

Get Perl Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.