Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: syntax for hashes with variable in name

by Tux (Abbot)
on Nov 22, 2010 at 15:39 UTC ( #872991=note: print w/replies, xml ) Need Help??

in reply to syntax for hashes with variable in name

Don't go that road. Please. Done that, been there. It leads to unwanted things, in the widest sense of "unwanted". Really!

Symbolic reference (where the name of the hash/array/scalar is in a variable name) can always be made strict and safer by embedding it inside a hash:

my $hash_name = "platform_$site"; %$hash_name = ( key => $value ); : my $value = ${$hash_name}{key};


my %global_hash; my $hash_name = "platform_$site"; $global_hash{$hash_name} = { key => $value }; : my $value = $global_hash{$hash_name}{key};

Or - even better - go with jethro's suggestion to use multidimensional data structures.

Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: syntax for hashes with variable in name
by aquarium (Curate) on Nov 23, 2010 at 02:44 UTC
    ...ditto...but once you've touched some code, you own it, and anything it may or may not do with good or bad data as input. so as much as this sounds as a bit of a pain, you're much better of re-writing the code to use good/clean programming patterns. code with symbolic reference variables is harder to debug/maintain/improve. i once wrote a data reporting perl script initially (and naively) using symbolic reference variables. i immediately re-wrote it using multilevel hashes, and the spaghetti code turned magically into nicely ordered simple subroutines and loops, and halved the number of lines of code.
    the hardest line to type correctly is: stty erase ^H

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://872991]
[Discipulus]: good night monks!
[shmem]: LanX: oh... now I have to figure out if this is good or bad news.
LanX bad day monks ?
[LanX]: hope you find sleep
[shmem]: LanX: which one?
[LanX]: the last
Discipulus yes a very bad day
[shmem]: grandola, vila morena
[shmem]: LanX: ah, that one. You'll get the bug reports also :P
[Discipulus]: upvote me! i'll make pm great again! ;=)

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2017-04-29 22:43 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (534 votes). Check out past polls.