Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Scope of lexical variables in the main script

by sophate (Beadle)
on Apr 20, 2012 at 05:52 UTC ( [id://966084]=note: print w/replies, xml ) Need Help??


in reply to Re: Scope of lexical variables in the main script
in thread Scope of lexical variables in the main script

Thanks, got it :-)

One more question about lexical variables. If I define a lexical variable in a block, what happens to the memory storing the value of the lexical variable after the code exits the block?

For example, in the script below. I cannot directly access $tricky outside the block. But it can be done with a reference. My question is if the memory storing the contents of $tricky has been released right after the block ends? If so, does it mean some other programs can write to that memory and when I try to retrieve the value of $tricky with a reference after the block, I may get something unexpected(memory got overwritten by other program)?

#!/usr/bin/pere my $reference; { my $tricky = "TRICKY"; $reference = \$tricky; } print "\$tricky is not defined\n" unless $tricky ; # What happens to the memory storing the contents of $tricky? # Has the memory storing $tricky been released? print "Using a reference, here you are: $$reference\n";

Replies are listed 'Best First'.
Re^3: Scope of lexical variables in the main script
by davido (Cardinal) on Apr 20, 2012 at 05:58 UTC

    Perl uses reference counting. In your case, inside the block,$tricky and $reference each own one reference to the scalar container that holds the string, "TRICKY". While inside the block, the reference count to the SV that holds the string "TRICKY" is two. After the block exits, $tricky goes out of scope, and the reference count drops by one, to one. As long as $reference remains in scope, the SV (the container for a scalar) holding the string "TRICKY" remains alive.

    After $reference passes out of scope, assuming nobody else holds a reference to that scalar, the ref-count drops to zero. Perl regains that memory as available for future use. It's not released back to the OS.

    Aren't you glad this isn't simple pointers in C or C++? :)


    Dave

Re^3: Scope of lexical variables in the main script
by Marshall (Canon) on Apr 20, 2012 at 06:33 UTC
    Perl uses reference counting. Every time the code goes over a "my" variable, you are going to get new memory unless Perl can reuse what it had before (and it will it if it can - but Perl doesn't do garbage collection like JAVA).

    If you pass a reference to that memory from the: my $trick = "TRICKY"; statement out of a sub, the next time Perl sees the "my", it allocates new memory for it if it sees that what it did before is still in "use" - meaning a reference to it exists. Perl "frees" memory back for its own use when it sees that the reference count to that memory is zero - not well illustrated in this example. The point here is that you get a completely new copy of "TRICKY" every time the sub is called.

    My verbage was a bit confusing, but does the code answer the question?

    #!/usr/bin/perl use strict; use warnings; my @tricks; for (1..3) { my $ref_to_a_trick = get_a_trick(); push @tricks, $ref_to_a_trick; } print @tricks,"\n"; #SCALAR(0x182b394)SCALAR(0x24920c)SCALAR(0x24925c) #note each "trick reference" points to a different location #@tricks is an array of scalar references foreach my $ref (@tricks) { print "$$ref\n"; #de-reference each "trick" to get the value #TRICKY #TRICKY #TRICKY } sub get_a_trick { my $trick = "TRICKY"; return (\$trick); }
    @tricks=(); will "free" the $tricks memory back to Perl (not to the O/S) and Perl will reuse it if it can because no references exists any longer to various memory allocations of "TRICKY".

      Thank you every one for the detailed explanation. Your prompt response really make me love Perl and PerlMonks more :-)

Re^3: Scope of lexical variables in the main script
by Anonymous Monk on Apr 20, 2012 at 06:07 UTC
Re^3: Scope of lexical variables in the main script
by nemesdani (Friar) on Apr 20, 2012 at 06:58 UTC
    And if you want to even more tricky, and thinking in OOP terms, you can weaken the reference (from perldoc: http://perldoc.perl.org/Scalar/Util.html):

    weaken REF REF will be turned into a weak reference. This means that it will not hold a reference count on the object it references. Also when the reference count on that object reaches zero, REF will be set to undef.

    This is useful for keeping copies of references , but you don't want to prevent the object being DESTROY-ed at its usual time.

    I'm too lazy to be proud of being impatient.

Log In?
Username:
Password:

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

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

    No recent polls found