Why is var using same memory location 
Author Message
 Why is var using same memory location

I don't get this.

#CODE START

    $tc = "a";  # This is a test

Quote:
}

#CODE END

Now after I run through this loop, my entire array is filled with
"a"'s.  So if I modify $tc, I'm actually modifying the array.  I don't
want this.


#CODE START
my $x = 0;

    $ENV{'TUXCONFIG'} = $_;
    $tuxconfig[$x] = [`tmunloadcf`];
    $x++;

Quote:
}

#CODE END

tmunloadcf is a UNIX program that returns a few hundered lines of code

I debugged this and \$tc has the same memory location as
\$tuxconfig[0][0].

I don't understand why.... can someone fill me in?

Regards,
Brian



Sat, 27 Dec 2003 03:34:12 GMT  
 Why is var using same memory location

  B> I don't get this.
  B> #CODE START

  B>     $tc = "a";  # This is a test
  B> }
  B> #CODE END

  B> Now after I run through this loop, my entire array is filled with
  B> "a"'s.  So if I modify $tc, I'm actually modifying the array.  I don't
  B> want this.

then don't mdoify $tc. foreach is DEFINED to alias its looping variable
to each element in turn just so you can do that. if you don't want to
have it modified, then why are you modifying it?

uri

--

SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Learn Advanced Object Oriented Perl from Damian Conway - Boston, July 10-11
Class and Registration info:     http://www.sysarch.com/perl/OOP_class.html



Sat, 27 Dec 2003 03:57:25 GMT  
 Why is var using same memory location

Quote:

> I don't get this.


>     $tc = "a";  # This is a test
> }

> Now after I run through this loop, my entire array is filled with
> "a"'s. So if I modify $tc, I'm actually modifying the array.

Correct.

Quote:
> I don't want this.

Then don't modify $tc, or make a copy and mangle that instead.

Irrelevant.

Quote:
> I debugged this and \$tc has the same memory location as
> \$tuxconfig[0][0].

Yep.

Quote:
> I don't understand why.... can someone fill me in?

This is a design feature of Perl. From the description of foreach in the
perlsyn manpage:

   If any element of LIST is an lvalue, you can modify it by
   modifying VAR inside the loop. That's because the foreach
   loop index variable is an implicit alias for each item in
   the list that you're looping over.

Most of the time, this is useful (or at least harmless). But
occasionally you *don't* want changes to propagate back, and in those
situations you have to takes steps to avoid it.

-mjc



Sat, 27 Dec 2003 04:31:18 GMT  
 Why is var using same memory location
Ahhh... thanks.  I was getting pretty confused.  I was starting to
question my understanding of references.
Quote:

> This is a design feature of Perl. From the description of foreach in the
> perlsyn manpage:

>    If any element of LIST is an lvalue, you can modify it by
>    modifying VAR inside the loop. That's because the foreach
>    loop index variable is an implicit alias for each item in
>    the list that you're looping over.

> Most of the time, this is useful (or at least harmless). But
> occasionally you *don't* want changes to propagate back, and in those
> situations you have to takes steps to avoid it.

> -mjc



Sat, 27 Dec 2003 23:04:49 GMT  
 Why is var using same memory location
This has nothing to do with your problem, but is a suggestion for an
improvement:

[snip]

Quote:

> #CODE START
> my $x = 0;

>     $ENV{'TUXCONFIG'} = $_;
>     $tuxconfig[$x] = [`tmunloadcf`];
>     $x++;
> }
> #CODE END

> tmunloadcf is a UNIX program that returns a few hundered lines of code


It's not perlish to use an index unless you *really* need it, for
example because you want to print out the index.  You would be better
off writing the above code as follows:

        local $ENV{TUXCONFIG} = $_;
        [`tmunloadcf`];

Or:

    local $ENV{TUXCONFIG} = $_;

Quote:
}

--
The longer a man is wrong, the surer he is that he's right.


Sun, 04 Jan 2004 10:34:35 GMT  
 Why is var using same memory location


Quote:
> This has nothing to do with your problem, but is a suggestion for an
> improvement:
> [snip]
> It's not perlish to use an index unless you *really* need it, for
> example because you want to print out the index.  You would be better
> off writing the above code as follows:


>         local $ENV{TUXCONFIG} = $_;
>         [`tmunloadcf`];

> Or:

>     local $ENV{TUXCONFIG} = $_;

> }

Thanks for the suggestion.  I might try the latter.. because I can't figure
out the map function.


Sun, 04 Jan 2004 22:30:20 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Why am I running out of memory?

2. How much memory am I using?

3. why is my perl script using so much memory

4. Save memory and measure used memory???

5. Save memory and measure used memory???

6. Perl HTML script stops on $var - why?

7. if ($_ =~ /^$var/) always fails - why?

8. newbie-perl or why am i blind to this simple

9. Why I am glad I know perl

10. Why am I dumping core???

11. ? Now I am reaaly perplexed, Why doesn't (INDEX) work

12. Why am I stuck?

 

 
Powered by phpBB® Forum Software