Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Changing variables be reference

by MMGM (Initiate)
on Mar 07, 2001 at 00:08 UTC ( [id://62566]=perlquestion: print w/replies, xml ) Need Help??

MMGM has asked for the wisdom of the Perl Monks concerning the following question:

Anyone know if I can accomplish the following:
$USERNAME = "my_name"; #init some vars $PARM1 = '$USERNAME'; $PARM2 = "other_user"; $PARM1 = \$PARM2; #$$PARM1 now is "other_user"
At this point, $USERNAME is still "my_name" and $PARM1 appears to be the scalar address. How can I get $USERNAME to be "other_user" without explicitly referencing $USERNAME? How do I appropriately setup that level of indirection? Thanks, -Michael

Replies are listed 'Best First'.
(jeffa) Re: Changing variables be reference
by jeffa (Bishop) on Mar 07, 2001 at 00:17 UTC
    Ah, symbolic references . . .

    Are BAD!! You shouldn't use them, but since you are, here is the answer:

    $USERNAME = 'my_name'; $PARM1 = 'USERNAME'; print "$$PARM1\n";
    Now you can access $USERNAME via $PARM1. You really should use strict, by the way - it's like making a hyper child sit on his/her hands. :)

    References are best used on lists and objects:

    my $array_ref = @foo; my $hash_ref = %bar; my $obj_ref = new Foo::Bar;
    so that when they get passed around, space and time are not wasted making copies. Instead, the address is passed instead of making an explicit copy. References to scalars are not very usefull.

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
Re: Changing variables be reference
by redcloud (Parson) on Mar 07, 2001 at 00:22 UTC
    How about:
    $USERNAME = "my_name"; $PARM1 = \$USERNAME; $PARM2 = "other_user"; $$PARM1 = $PARM2;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-19 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found