Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Dynamic names

by ELISHEVA (Prior)
on Nov 17, 2010 at 05:16 UTC ( #871921=note: print w/replies, xml ) Need Help??

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!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://871921]
What's the matter? Cat got your tongue?...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2018-04-19 18:53 GMT
Find Nodes?
    Voting Booth?