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:

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


GreetZ!,

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

In reply to How Tied/Tight is it ? by ChOas

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.