Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Dynamic Variable Names?

by RollyGuy (Chaplain)
on Jul 23, 2002 at 15:34 UTC ( #184452=note: print w/replies, xml ) Need Help??

in reply to Dynamic Variable Names?

Try using eval. I've done this with function names before. Your code will look like:
I'm trying to do something like this: my @fee; for (my $i=0; $i < $MAX_ROWS; $i++){ my $eval_str = "push @fee, \$foo_$i"; eval($eval_str); }#end loop

Here I create the command I want to execute as a string and then evaluate it with eval.
UPDATE: I just saw dragonchild's comments and realized that my method probably wouldn't be safe for web applications. The eval would be dangerous, so use at your own risk.

Replies are listed 'Best First'.
Re: Dynamic Variable Names?
by Abigail-II (Bishop) on Jul 23, 2002 at 15:58 UTC
    Except from the fact the eval contains a syntax error (you'd need to escape the @), what makes you think the eval is unsafe? There's no data coming from the outside. The eval is as safe as any other code from the programmer.

    There's a lot that can be argued against "variable names inside variable names", but security is only an argument if you use data from the untrusted environment as variable names.


      ... but security is only an argument if you use data from the untrusted environment as variable names.


      Whenever code does not behave as expected, there is a real possibility of an unexpected consequence that compromises security. Therefore any programming practice that leads to bugs is also a source of potential security flaws, even if it is not obvious how to get there.

        The only coding practice that doesn't lead to bugs is to not code at all. eval()s are as dangerous as code written with vi - you can make the same mistakes.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2022-08-15 02:47 GMT
Find Nodes?
    Voting Booth?

    No recent polls found