The Perl Jargon : Stashes

rafael on 2003-06-19T12:22:08

I decided to begin a series of posts about some of the interesting words of the Perl jargon.

Today : What's a stash ?

In common english, a stash is a hidden place, used to secretly store precious things. So what's a stash in Perl ? Well, it's more or less the same thing : a hidden data structure, used to store precious things. It's also an acronym for Symbol Table hASH : the internal hash where all your global variables live. (This double meaning makes this word particularly well-crafted.) Each package Foo defined into a perl program has its own stash, %Foo:: (note the trailing double colon.) The keys of the hash are the symbol names, and the values are the globs corresponding to those values. Here's an example :

$Foo::x = 42;
print "$_,$Foo::{$_},${$Foo::{$_}}\n" for keys %Foo::;
This small snippet outputs x,*Foo::x,42.

Note that stashes can live in other stashes, as demonstrated by the following code :

$Foo::Bar::x = 42;
print "$_,$Foo::{$_}\n" for keys %Foo::;
which outputs Bar::,*Foo::Bar::, demonstrating that %Foo::Bar:: is a sub-stash of %Foo::. Notably, %main:: is a sub-stash of the root stash %::, but as both refer in fact to the same stash, that implies that you need to be careful if you ever want to recursively walk the stashes.

Another interesting stash is %CORE::GLOBAL::, where live the subroutines that override perl keywords.


Quick, hide your stash, the cops are coming!

Dom2 on 2003-06-19T13:01:47

I always think of a stash as a place to keep your (probably illegal) drugs in. But maybe I've read too many Fabulous Furry Freak Brothers comix...

-Dom

Re:Quick, hide your stash, the cops are coming!

rafael on 2003-06-19T13:17:24

And indeed : next week, we'll speak about cops in perl. (Not kidding.)

kudos

geoff on 2003-06-19T14:18:16

these perl internals tidbits are nice. kudos and keep up the good work.

perlglos

Juerd on 2003-06-19T14:47:56

I once started a perlglos page, with short to-the-point definitions of common Perl programming terms. html version is available, POD is somewhere, but I don't know where :)

Re:perlglos

koschei on 2003-06-20T00:02:06

Should probably submit that to p5p or perl-documentation@perl.org --- I think a filled out version would be good for the core pod.

treasures in blog

properler_head on 2003-06-20T12:07:43

sometimes with too much time on his hand should centralize these all too precious "what the fuck is" like yours or Dan's ones.