[Chicago-talk] More than just S&M in perl...

Steven Lembark lembark at wrkhors.com
Tue Sep 7 16:47:29 CDT 2004

You can now be much more strict as well.

Or should I start in with NEXT:: of kin jokes instead?

In any case:

5.8.5 came out with Symbol in the standard package.

Rather minimalist interface and doc's make it harder
to use, but it's worth getting into. Main use is allowing
you to generate globs on the fly without resorting to
no strict refs.

For example, I just used it to purge the last unstricts
from NEXT::init.

For example:

	my $ISA  = *{qualify_to_ref 'ISA',  $caller}{ARRAY};
	my $init =   qualify_to_ref 'init', $caller;
	my $meta =   qualify_to_ref 'meta', $caller;

	unshift @$ISA, __PACKAGE__;

	*$meta = $ref;

	*$meta = sub { $ref };

	*$init = $sub;

This is also useful for probing a caller's namespace
for variables. Somewhat contrived example, but if you
don't know whether the caller has a hash or an array
in it for some variable you could use:

	sub blessed_isa
		my $obj = shift;
		my $name = shift;

		my $pkg  = ref $obj || $obj;

		my $glob = qualify_to_ref $name, $pkg;

		my @found = '';

		defined *$glob{$_} && push @found, $_
			for qw(HASH ARRAY CODE);


Checking for $glob{CODE} is useful for looking up
methods defined in the class (vs. inherited as with

Or, say you want to pass the type of data required into
a constructor only if the data doesn't already exist:

	my $item = shift;

	my $type = uc shift;

	my $pkg = ref $item || $item;

	my $glob = qualify_to_ref $name, $pkg;

	*$glob = $type eq 'HASH' ? {} : []
		unless defined *$glob{$type};

Nice thing is that you can do all of this now without resorting
to no strict 'refs'.

Steven Lembark                                       85-09 90th Street
Workhorse Computing                                Woodhaven, NY 11421
lembark at wrkhors.com                                     1 888 359 3508
-------------- next part --------------
A non-text attachment was scrubbed...
Name: NEXT-init-1.00.tar.gz
Type: application/x-gunzip
Size: 6961 bytes
Desc: not available
Url : http://mail.pm.org/pipermail/chicago-talk/attachments/20040907/31ecea16/NEXT-init-1.00.tar-0001.bin

More information about the Chicago-talk mailing list