Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Using a varabile contained within another variable?

by blax (Sexton)
on Sep 21, 2001 at 09:51 UTC ( [id://113805]=perlquestion: print w/replies, xml ) Need Help??

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

In the following code:
my $x = 1; my $y = '$x';
is there anyway to manipulate $x by using $y?

Thanks in advance,
blax

Replies are listed 'Best First'.
Re: Using a varabile contained within another variable?
by blakem (Monsignor) on Sep 21, 2001 at 10:02 UTC
    I'm going to assume this is an "is it possible" rather than an "is it a good idea" question.... You could do something evil with eval, like so:
    #!/usr/bin/perl -wT use strict; my $x = 1; my $y = '$x'; eval "$y = 6"; print "X:$x\n"; print "Y:$y\n";
    What do you think the output of the above script is?

    Oh, and Sweeper reminded me in another thread of a USENET post by dominus called Why it's stupid to `use a variable as a variable name'

    -Blake

Re: Using a varabile contained within another variable?
by lemming (Priest) on Sep 21, 2001 at 10:03 UTC
    Here's one way
    my $x = 1; my $y = '$x'; eval "$y++"; print $x, "\n"; # Output == 2
Re: Using a varabile contained within another variable?
by Zaxo (Archbishop) on Sep 21, 2001 at 10:08 UTC
Re: Using a varabile contained within another variable?
by ducky (Scribe) on Sep 21, 2001 at 10:09 UTC

    Well, while I would recommend using different approaches to something like this, soft references seems like what you're heading for:

    my $x = 1 ; my $y = 'x' ; $$y++ ; # $x now == 2

    You can't use this while using strict... most would consider that a feature =) Like I said, I'd try to find another way to approach whatever problem you're coding for. Since I don't know anything about it (perhaps this is just a learning exercise), I can't begin to offer more help in that regard.

    If you do have a particular problem you're coding for, feel free to ask about alternative methods. TMTOWTDI, after all.

    HTH

    -Ducky

Re: Using a varabile contained within another variable?
by mitd (Curate) on Sep 21, 2001 at 10:25 UTC
    mitd is a very bad boy, a very bad boy is he. Quick nobody's watching
    no strict 'refs'; $x = 1; $y = "x"; $$y++; print $x," bad!\n";
    2 bad!

    I spit 'use stricts' in your face...

    mitd-Made in the Dark
    'My favourite colour appears to be grey.'

Re: Using a varabile contained within another variable?
by Chady (Priest) on Sep 21, 2001 at 09:55 UTC

    First of all, in this piece of code, $y doesn't have anything to do with $x because it's single quoted, so $y contains a string which looks like $x

    You might want to ref:

    my $x = 1; my $y = \$x; $$y++; print $x;

    Update: It turned out that $y has something to do with $x after all.. my bad. see lemming's post below..


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/

Log In?
Username:
Password:

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

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

    No recent polls found