> Or am i missing the point?

Yeah, I think you did.

In the first example:
> $var_name = $1; $value = $2;
> $$var_name = $value;     # Or more compactly, $$1 = $2;

The variable *name* is dynamic (which is usually a bad practice).

In the second:
> $level = $2;

Here the variable name is hard-coded.

This is the real point...

$x = "foo";
$$x = 20;    # this sets $foo
print $foo;  # prints "20"
print $x;    # still prints "foo"!.

But like I said, this is usually a bad way of doing things.  It is better to
use a hash when you have dynamic names.

Rob


-----Original Message-----
From: Kipp, James [mailto:[EMAIL PROTECTED]
Sent: Thursday, July 31, 2003 2:27 PM
To: [EMAIL PROTECTED]
Subject: Why use symbolic refs?


In the "Advanced Perl book" it gives an example of using sym ref:
--
process a command-line option such as "-Ddebug_level=3" and set the
$debug_level variable. This is one way of doing it:

while ($arg = shift @ARGV){
    if ($arg =~ /-D(\w+)=(\w+)/) {
         $var_name = $1; $value = $2;
         $$var_name = $value;     # Or more compactly, $$1 = $2;
    }
}
----
how is this any better than:

while ($arg = shift @ARGV){
    if ($arg =~ /-D(\w+)=(\w+)/) {
         $level = $2;
       }
}
---

Or am i missing the point? I never use sym refs and just wondering why i
would ever want to

Thanks
Jim
 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to