Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: another 'array to hash' question

by davido (Cardinal)
on Dec 10, 2003 at 17:17 UTC ( [id://313776]=note: print w/replies, xml ) Need Help??


in reply to another 'array to hash' question

The title of your node is "another 'array to hash' question", yet you ask:

# I have my ($fn, $ln, $age) = qw(Joe McCarty 89); # I want my %crazyperson = ( 'fn' => 'Joe', 'ln' => 'McCarty', 'age => 89);

...which has to do with a list of scalars, not an array. It seems like you're asking "How do I convert a list of scalar variables and their values into a hash of keys (by the same names) and values?"

Perhaps this is all you really need:

my %crazyperson; @crazyperson{'fn', 'Joe', 'ln'} = qw/Joe McCarty 89/;

...which uses a hash slice.

On the other hand, the question goes on to imply that you need to look up the symbol name of a variable. Which implies that you're trying to do something with symbolic references. Maybe, in that case, it would be easier to start out with the symbol names, and coerce them into scalar variable names, and use them as hash key names. But the whole thing sounds like madness, which is one of the best reasons for avoiding symbolic references in the first place. Of course another good reason for avoiding symbolic references is, for example:

$name = "Ted"; while ( my $line = <DATA> ) { chomp $line; my ( $symbol, $value ) = split /,/, $line; ${$symbol} = $value; } print $name, "\n"; __DATA__ name,Frank __OUTPUT__ Frank

What you've now done is allowed an external source to define the name of a global scalar variable within your script. It happens in this case, that the external source (__DATA__) has a symbol called "name". You're now in trouble, because you alreay were using $name to hold "Ted". You've just clobbered that, and given $name the value "Frank" instead.

By allowing an outside source free reign over names of global variables inside your script, you stand a pretty good chance of eventually allowing that outside source to clobber a variable you're already using for something else.

Consider, for example, that you've set a flag variable called, "$is_valid_user". And unless the flag is 'true', the user is denied access to something dangerous. Well, what if that user is allowed, through the magic of symbolic references from outside sources, to hand your script a true value for $is_valid_user? Symbolic references open up this sort of issue, if used uncarefully.

So the point is, not only is willy-nilly use of symbolic references "a road to madness", it's also insecure. And those are two very good reasons for not trying to get around the strictures imposed by the use strict; pragma.

Update: You may be interested to know that the global symbol table (the names of all of your script's global variables) is, itself, held in a hash to which you have access. The hash is %::

I mention this as an argument for convincing you that rather than using symbolic references to create and manipulate a bunch of entries in the %:: hash (your global symbol table), you should just use your own hash in the first place, avoiding pure scalar variables any time you start thinking, "Here's somewhere I need a symbolic ref." Using your own hash instead of your script's global symbol table hash, eliminates the potential for an outside source clobbering a variable you expect to be using for something else.

There are legitimate needs for symbolic references, but those needs are quite advanced. You're almost always better off treating name/value pairs as hash elements from the outset rather than trying to create a symbolic reference out of the name.


Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-03-28 17:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found