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).


In reply to Re: Symbol table manipulation problem by ig
in thread Symbol table manipulation problem by arthurg

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.