Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re: another 'array to hash' question by davido
in thread another 'array to hash' question by punkish

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-24 18:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found