Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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
Re^2: Dynamic names
by yoda54 (Monk) on Nov 19, 2010 at 01:27 UTC
    Thanks for the post!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://871921]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2014-07-12 09:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (239 votes), past polls