Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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(); }

Comment on Re: Dynamic names
Download Code
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]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (13)
As of 2015-11-30 18:40 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (778 votes), past polls