Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Re: another 'array to hash' question

by punkish (Priest)
on Dec 10, 2003 at 16:04 UTC ( [id://313753]=note: print w/replies, xml ) Need Help??


in reply to Re: another 'array to hash' question
in thread another 'array to hash' question

>>Any pointers to reading lucid descriptions of dealing
>>with variable variables in perl would be greatly appreciated.

>In short, you don't.

why not? Because it is bad? Because it is bad style? Because it will lead to unexpected results? I don't mean to be argumentative, but just telling me I shouldn't is not satifying.

>Why is it that so many people want to
>use symbolic references in Perl (because that's what it is)?
>You don't have symbolic references in C. You don't have
>symbolic references in Java. You don't have symbolic
>references in many, many languages. And I don't get the
>impression people programming in Java or C regulary ask
>how to get the name of a variable, or how to use a value
>as a variable.

I know nothing about C and Java so I have no idea what those users do. However, I can think of various uses for such a facility. Suppose I have a choice of actions I can pick from

$action can be 'dothis' or 'dothat' or 'dosomethingelse'

And I want to run the functions dothis() or dothat() or dosomething() based on the action picked. I can use the following code --
&dothis if ($action eq 'dothis'); &dothat if ($action eq 'dothat'); &dosomethingelse if ($action eq 'dosomethingelse');
or I could just
&$action;
but I can't because that is not allowed under use strict; so I
eval($action);
and that works and is a lot shorter than the series of if statements. Imagine if I had a lot of choices for action -- that would be a lot of if statements.

Well, this is just one example.

>>what if the var was a var?
>What else could a var be than a var

Sorry, my bad. I meant, what if the var name was varying and I wanted to do something based on its name.

Anyway, thanks for the advice. I'll think of better ways to write my code.

Replies are listed 'Best First'.
Re: another 'array to hash' question
by Abigail-II (Bishop) on Dec 10, 2003 at 16:26 UTC
    why not? Because it is bad? Because it is bad style? Because it will lead to unexpected results? I don't mean to be argumentative, but just telling me I shouldn't is not satifying.
    Well, you already indicated that you saw the musings of Mark Jason Dominus about this subject. Perhaps you should reread them. He did a much better job than I can.

    or I could just
    &$action;
    but I can't because that is not allowed under use strict; so I
    eval($action);
    That's just plain stupid. If you really want to do something that strict prevents you from doing, you shouldn't code your way around it - just turn strictness (locally) off. Using eval in this case, is like entering the house through the chimney, because the door is closed, but unlocked.
    Imagine if I had a lot of choices for action -- that would be a lot of if statements.
    No. You'd use a reference to the function you want to execute, or you use a hash. &$action could be dangerous. What if $action contains a string for which there isn't a subroutine by that name? Or worse, a name of a subroutine which really shouldn't be called right now? Please reread Mark Jasons articles again. They explain the dangers.

    Note that I'm not saying that you should never use symbolic references. But as long as you don't understand the dangers, don't use them. Don't play with matches before you understand fire either.

    Abigail

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-28 13:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found