| CARVIEW |
Tricks of the Wizards
(Perl Tricks and Programming Technique)
Length: 3 hours
Prerequisites: Attendees should have basic familiarity with Perl's packages, references, modules, and objects, and should desire to become wizards.
Description
New and Improved for 2003!
This class will explore Perl's most unusual features. We'll look at some of the standard modules written by famous wizards like Tom Christiansen, Damian Conway, and Larry Wall, and learn what they're for and how they work.
First we'll investigate Perl's remarkable 'glob' feature. We'll see many uses of globs, including the 'Exporter' module, which everyone uses but hardly anyone understands. We'll discuss how to accomplish the same globby magic in Perl 6, which won't have globs.
After this we'll look at unusual uses of Perl's tie function, which scoops the brain out of an ordinary Perl array, hash, or filehandle, replacing it with your own concoction. We'll make hashes with case-insensitive keys, arrays that mirror the contents of a file, and filehandles that suppress annoying output.
Then we'll learn about AUTOLOAD, Perl's function of last resort. We'll see a tremendously useful application: How to generate the accessor methods of a class without writing pages of repetitive code. We'll see how Larry's Shell module uses AUTOLOAD to emulate the Unix shell inside Perl scripts, and how Damian Conway's NEXT module uses AUTOLOAD for method redispatch.
Section 4 discusses Perl's new "source filter" feature. This magic allows you to write Perl programs in any language, and translate them to Perl at the last moment. We'll add a switch statement to Perl and we'll see how Perl 5 can emulate the variable syntax of Perl 6.
The class will finish with nine very small but useful enchantments that take thirty seconds each.
Outline
- A detailed explanation of globs, including the internals of the Perl symbol table, what globs are and what they can be used for, and how Exporter.pm works We'll also look briefly at how these techniques will translate into Perl 6.
- Principles Of Magic
- The Magic Path to Enlightenment
- Globs
- Operations with Globs
- Aliasing
- Exportation
- Forced Importing / Aliasing
- (No) Globs in Perl 6
- Passing Filehandles
- Globjects
- Wrappers
- Stash Walking
- Read-Only Constants
- Templating systems
- tie, including the combination of tie with the exporter to create a module that exports a `magical' variable, and use of tied filehandles to evade a poor interface design in a module.
- tieing generally
- Ties: Trivial (Annoying) Example ($cia)
- How to Tie
- Tied Scalar Example
- Tied Hash Example
- CGI.pm
- Exporting a Tied Variable
- Config.pm and %Config
- Magical Exporter Variable
- Exporting a Magical Variable
- Magical Exporter Variable
- Tied Arrays
- Tied Filehandles
- Missing tie Methods
- The Mother of All Bizarre tie Tricks
- AUTOLOAD, with such common uses as method generation, autosplitting, and selfloading.
- What is Autoloading?
- Simple AUTOLOAD Example
- Magic goto
- Tracing Again
- Case-Insensitive Function Calls
- Function Call Caching
- Typical AUTOLOAD Use: Accessor Methods
- Direct Emulation of Accessors
- Caching Accessor Methods
- Autoloading From a File
- Generating Functions Dynamically
- NEXT.pm
- Shell.pm
- Source filters, with typical and atypical uses, including a
digression showing how to write a program that compiles
differently when the moon is full
- What's a Filter?
- Filter::Simple
- Filter::Util::Call
- "Only perl can parse Perl"
- Function Tracing Again
- Internationalization
- Perl6::Variables
- Cantrips: Nine quick and easy hacks that use Perl's unique features to perform common small operations such as combining two lists or loading a function on demand.
- Returning a False Value
- The Self-Replacing Stub
- Schwartzian Transform
- Debug Printing of Strings
- Debug Printing of Lists
- ?:?:?:
- Booleanumbers
- Local Effects
- Selecting n Different Things
Sample Slides
8 15 37 71 88 94 107 122Some of these things might sound like things you have heard of before. But they are likely to be things that you haven't heard of before. With a few exceptions, I try to put a really strange twist on everything, even familiar tasks. The idea is that even if you don't want to do it my way, you have something to think about that might turn out to be useful in some other way.
The talk has evolved substantially over the years. Section 3 used
to be about Perl's operator overloading feature, but the autoloading
turned out to be more interesting, so I replaced it. Section 4 used
to be titled Large Techniques; it was replaced with a new
Source Filters section in 2003. All the slides for the
removed sections are still in a 'bonus section' at the end of the
printed materials. Corporate clients may request reinsertion of any
of these sections.
Also new in 2003: I need no longer issue a disclaimer that I am merely passing on the tricks of the wizards, and I do not claim to be a wizard myself. As of summer 2002, I am a wizard.
Illustrations on this page are by W. W. Denslow.
Return to: Universe of Discourse main page | Perl Paraphernalia | Other Classes and Talks
mjd-perl-yak+@plover.com