http://www.perlmonks.org?node_id=871921


in reply to Dynamic names

I'm not entirely clear on what you want to accomplish, but maybe what you meant was that you want the token type of each token to trigger certain processing? For example, if you have a name "foo" and your parser says it is a variable declaration token, you want to be able to trigger certain processing rules depending on whether that declaration is for an integer, email address, day of week, or some other special type?

If you need to trigger special processing based solely on the combination of a name and a type, you could use a slightly modified version of cdarke 's solution:

use strict; use warnings; package Cow; { sub speak { print ${$_[0]}, " says Moo\n" } } package Horse; { sub speak { print ${$_[0]}, " says 'Hi, my name is Ed'\n" } } package Sheep; { sub speak { print ${$_[0]}, " says Baaaah\n" } } # mapping names to types my %Pasture = ( Bessie => 'Cow', , Annie => 'Cow', , Joe => 'Horse' , Lily => 'Sheep' , James => 'Sheep' ); while (my ($name, $animal) = each(%Pasture)) { # assign a class to each name based on type my $oAnimal = bless(\do{my $anon=$name;}, $animal); # do processing that involves both name and type $oAnimal->speak(); }

Replies are listed 'Best First'.
Re^2: Dynamic names
by yoda54 (Monk) on Nov 19, 2010 at 01:27 UTC
    Thanks for the post!