Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How Tied/Tight is it ?

by ChOas (Curate)
on May 08, 2001 at 14:17 UTC ( [id://78786]=perlquestion: print w/replies, xml ) Need Help??

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

Hello all,

I`m experimenting with tie, and packages, and I have a question...

I think the question is about how tight a variable is tied (lexical tie,
not the function) to the scope of a for loop...

...God, I hope this will make sense to you all...

This is the package 'Spy':
===
package Spy; use strict; sub TIESCALAR { my ($Pkg,$Name,$Val)=@_; my $Obj=[$Name,$Val]; return bless $Obj,$Pkg; } sub FETCH { my ($Obj)=@_; print STDERR "Fetched ",$Obj->[1]||'undef'," from ",$Obj->[0],"\n"; return $Obj->[1]; } sub STORE { my ($Obj,$Val)=@_; print STDERR "Stored ",$Val||'undef'," in ",$Obj->[0],"\n"; return $Obj->[1]=$Val; } 1;
===
And this is the test code:
===
#!/usr/bin/perl -w use strict; use Spy; my ($Var1,$Var2); tie $Var1,'Spy','Var1'; tie $Var2,'Spy','Var2'; ($Var1,$Var2)=(37332,666); print "Assigning \$Var2 to \$Var1, result: ", $Var1=$Var2,"\n"; # Humming 'Twilight Zone' for $Var2 (1..5) { $Var1=$Var2; print STDERR "$Var1\n"; $Var2="Test"; }; print "$Var2\n";
===
And here is the output:
martijnb@xxx$ ./test Stored 37332 in Var1 Stored 666 in Var2 Fetched 666 from Var2 Stored 666 in Var1 Fetched 666 from Var1 Assigning $Var2 to $Var1, result: 666 Stored 1 in Var1 Fetched 1 from Var1 1 Stored 2 in Var1 Fetched 2 from Var1 2 Stored 3 in Var1 Fetched 3 from Var1 3 Stored 4 in Var1 Fetched 4 from Var1 4 Stored 5 in Var1 Fetched 5 from Var1 5 Fetched 666 from Var2 666

Now... I CAN imagine that before the `for` loop
`something` is done that localizes $Var2 to the loop (as seen in the
output)...

Two questions though:
    Why don`t I see this localization ? (Too deep for tie ?)
    Is there any other way to keep monitoring $Var2, without
    assigning it to another monitored variable (like here $Var1) ?

I really hope all this made sense to you guys....


GreetZ!,
    ChOas

print "profeth still\n" if /bird|devil/;

Replies are listed 'Best First'.
Re: How Tied/Tight is it ?
by jbert (Priest) on May 08, 2001 at 18:09 UTC
    The behaviour makes sense if you remember that *assigning* to a foreach loop index changes the value in the array you are looping over. i.e.
    my @squares = (1, 2, 3, 4, 5); # Those aren't squares! foreach $i ( @squares ) { $i = $i * $i; } # Oh yes they are... print join ", ", @squares;
    So the underlying data you have named by '$Var2' isn't being set to each value in the loop - instead the name '$Var2' is being used for a different purpose inside the loop - to refer to the elements you loop over.
Re: How Tied/Tight is it ?
by suaveant (Parson) on May 08, 2001 at 17:31 UTC
    If I understand it correctly, for and foreach (same thing) use your $Var2 as a localized alias to the items in the list as they come through... so as far as I know there is no way to do exactly what you want... you could do
    for (1..5) { $Var2 = $_; $Var1=$Var2; print STDERR "$Var1\n"; $Var2="Test"; }
    The localization is just an automatic thing... it's a feature of for loops :)
                    - Ant

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found