Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Symbol table manipulation problem

by ig (Vicar)
on May 31, 2011 at 17:30 UTC ( #907509=note: print w/ replies, xml ) Need Help??


in reply to Symbol table manipulation problem

It will probably help you to read perlref carefully, and maybe even http://blob.perl.org/tpc/1998/Perl_Language_and_Modules/Perl%20Illustrated/ and perlguts, though the latter is a bit difficult.

The lines

no strict 'refs'; foreach my $F (@$Fields) {*{$F} = \ $_t->{$F}->[$_r]};

alias global variables in package main. After this, those variables are aliases to the elements of the table data structure. When you restore the saved values, you are updating the elements of the table data structure, via the aliases. This is why you see the last row of your table modified (and I am guessing not only the last row, but I didn't run your code).

What you need to do (I think - but test carefully) is make aliases to the original scalars and restore them to the globs (again, see perlref for what this means).

Here is an example of how you might do this:

use strict; use warnings; use Devel::Peek; our ($a, $b, $c); my @fields = qw(a b); print "\nBefore setting a value for \$a:\n"; Dump($a); $a = 'Test'; $b = 'Unused'; print "\nAfter setting a value for \$a:\n"; Dump($a); $c = 'Clobbered'; print "\nAnd \$c is:\n"; Dump($c); print "\nGlobref \*a is:\n"; Dump(*a); { no strict 'refs'; my %tmp; $tmp{$_} = \${$_} for(@fields); print "\nAfter saving references \\\%tmp is:\n"; Dump(\%tmp); *{'a'} = \$c; print "\nAfter aliasing \$a to \$c, \$a is:\n"; Dump($a); print "\nAfter aliasing \$a to \$c, \*a is:\n"; Dump(*a); *{$_} = $tmp{$_} for(@fields); print "\nAfter restoring \*a, \*a is:\n"; Dump(*a); } print "\nFinally, \$a is:\n"; Dump($a);

Which produces:

Before setting a value for $a: SV = NULL(0x0) at 0x8ecfa60 REFCNT = 1 FLAGS = () After setting a value for $a: SV = PV(0x8ebe0c0) at 0x8ecfa60 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x8ed8618 "Test"\0 CUR = 4 LEN = 8 And $c is: SV = PV(0x8ebe0d0) at 0x8edf228 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x8ed4b88 "Clobbered"\0 CUR = 9 LEN = 12 Globref *a is: SV = PVGV(0x8ef2840) at 0x8ecfa50 REFCNT = 10 FLAGS = (MULTI) NAME = "a" NAMELEN = 1 GvSTASH = 0x8ec0048 "main" GP = 0x8ed8c28 SV = 0x8ecfa60 REFCNT = 1 IO = 0x0 FORM = 0x0 AV = 0x0 HV = 0x0 CV = 0x0 CVGEN = 0x0 LINE = 8 FILE = "./test.pl" FLAGS = 0x2 EGV = 0x8ecfa50 "a" After saving references \%tmp is: SV = IV(0x8ec0e14) at 0x8ec0e18 REFCNT = 1 FLAGS = (TEMP,ROK) RV = 0x8f01bf8 SV = PVHV(0x8ec5454) at 0x8f01bf8 REFCNT = 2 FLAGS = (PADMY,SHAREKEYS) ARRAY = 0x8ecf330 (0:6, 1:2) hash quality = 125.0% KEYS = 2 FILL = 2 MAX = 7 RITER = -1 EITER = 0x0 Elt "a" HASH = 0xca2e9442 SV = IV(0x8ec0e24) at 0x8ec0e28 REFCNT = 1 FLAGS = (ROK) RV = 0x8ecfa60 SV = PV(0x8ebe0c0) at 0x8ecfa60 REFCNT = 2 FLAGS = (POK,pPOK) PV = 0x8ed8618 "Test"\0 CUR = 4 LEN = 8 Elt "b" HASH = 0xdb819b SV = IV(0x8ec0e44) at 0x8ec0e48 REFCNT = 1 FLAGS = (ROK) RV = 0x8ecfa90 SV = PV(0x8ebe0c8) at 0x8ecfa90 REFCNT = 2 FLAGS = (POK,pPOK) PV = 0x8ee98b8 "Unused"\0 CUR = 6 LEN = 8 After aliasing $a to $c, $a is: SV = PV(0x8ebe0d0) at 0x8edf228 REFCNT = 2 FLAGS = (POK,pPOK) PV = 0x8ed4b88 "Clobbered"\0 CUR = 9 LEN = 12 After aliasing $a to $c, *a is: SV = PVGV(0x8ef2840) at 0x8ecfa50 REFCNT = 10 FLAGS = (MULTI) NAME = "a" NAMELEN = 1 GvSTASH = 0x8ec0048 "main" GP = 0x8ed8c28 SV = 0x8edf228 REFCNT = 1 IO = 0x0 FORM = 0x0 AV = 0x0 HV = 0x0 CV = 0x0 CVGEN = 0x0 LINE = 8 FILE = "./test.pl" FLAGS = 0x2 EGV = 0x8ecfa50 "a" After restoring *a, *a is: SV = PVGV(0x8ef2840) at 0x8ecfa50 REFCNT = 10 FLAGS = (MULTI) NAME = "a" NAMELEN = 1 GvSTASH = 0x8ec0048 "main" GP = 0x8ed8c28 SV = 0x8ecfa60 REFCNT = 1 IO = 0x0 FORM = 0x0 AV = 0x0 HV = 0x0 CV = 0x0 CVGEN = 0x0 LINE = 8 FILE = "./test.pl" FLAGS = 0x2 EGV = 0x8ecfa50 "a" Finally, $a is: SV = PV(0x8ebe0c0) at 0x8ecfa60 REFCNT = 1 FLAGS = (POK,pPOK) PV = 0x8ed8618 "Test"\0 CUR = 4 LEN = 8

Have a read (and a play with) Devel::Peek, which I recommend to you as an excellent tool to test your understanding of globs and references, then work your way through the output of the test program. You will see that in the end $a refers not only to a string with the same content, but to the actual original string. This would be important if the originals had magic (tied variables, for example).


Comment on Re: Symbol table manipulation problem
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://907509]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-08-23 19:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (178 votes), past polls