Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Re: Re: undefining one slot of a typeglob

by gmpassos (Priest)
on Feb 24, 2004 at 20:00 UTC ( [id://331507]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: undefining one slot of a typeglob
in thread undefining one slot of a typeglob

"Symbol::gensym does create its symbols in the Symbol:: package, but then removes them from the package, so when a returned symbol goes out of scope it does get garbage collected"

Are you sure about that? I can guarantee to you that it won't make garbage collection! First because we don't have any note about that in the POD of Symbol, and because gensym() just return a reference of a GLOB in the package Symbol::, and any GLOB reference doesn't have scope, since it's a GLOBAL "object". The best way is to see the source of gensym():

sub gensym () { my $name = "GEN" . $genseq++; my $ref = \*{$genpkg . $name}; delete $$genpkg{$name}; $ref; }

Soo, when the $ref goes out, the GLOB still exists in the package Symbol::.

Here's a test of that:

{ my $glob = gensym() ; ${*$glob} = 123 ; print "SCOPED: " . "${*$glob}\n" ; } print "OUT: " . ${*Symbol::GEN0} . "\n";
And the output:
SCOPED: 123 OUT: 123

About the undef of types, well, this was already discussed here in PM, and is all about memory usage. If we make a undef in all the different types of a GLOB to clean a package from the memory, we will free much more memory than just make a undef *GLOB. And the memory difference is big. In my tests after 1000 executions I have the interpreter with 8Mb, and with the simple undef *GLOBS I have 150Mb!

Graciliano M. P.
"Creativity is the expression of the liberty".

Replies are listed 'Best First'.
Re: Re: Re: Re: undefining one slot of a typeglob
by ysth (Canon) on Feb 25, 2004 at 01:28 UTC
    { my $glob = gensym() ; ${*$glob} = 123 ; print "SCOPED: " . "${*$glob}\n" ; } print "OUT: " . ${*Symbol::GEN0} . "\n";
    I don't think you quite understand what that is doing. At compile time, that *Symbol::GEN0 in the OUT line causes the GEN0 glob to be created and referred to directly by the compiled code. Then at runtime, gensym fetches and deletes it from the symbol table (skipping creating it since it's already there). Adding to your test:
    use Symbol; print "Symbols: ", join(",",keys %Symbol::), "\n"; { my $glob = gensym() ; ${*$glob} = 123 ; print "SCOPED: " . "${*$glob}\n" ; } print "OUT: " . ${*Symbol::GEN0} . "\n"; print "Symbols2: ", join(",",keys %Symbol::), "\n"; print "OUT2: ", ${"Symbol::GEN0"} . "\n";
    gets you:
    Symbols: qualify,geniosym,gensym,GEN0,EXPORT,EXPORT_OK,import,ungensym +,qualify_to_ref,EXPORT_FAIL,delete_package,ISA,BEGIN,VERSION SCOPED: 123 OUT: 123 Symbols2: qualify,geniosym,gensym,EXPORT,EXPORT_OK,import,ungensym,qua +lify_to_ref,EXPORT_FAIL,delete_package,ISA,BEGIN,VERSION OUT2:
    About the undef of types, I don't remember ever seeing your actual tests, and I wasn't able to deduce anything useful from your Safe::World. If undefing the individual parts of the glob is freeing more than undefing the whole glob, it is a bug and sample code showing it in action would be very welcome.
Re: Re: Re: Re: undefining one slot of a typeglob
by ambrus (Abbot) on Feb 25, 2004 at 15:31 UTC

    I think the symbol gets garbage collected on newer Perls, but on older ones, you must explicitly ungensym it.

    Update: Yes, here's what it says:

    from perldoc Symbol:

    "Symbol::gensym" creates an anonymous glob and returns a reference to it. Such a glob reference can be used as a file or directory handle.

    For backward compatibility with older implementations that didn't support anonymous globs, "Symbol::ungensym" is also provided. But it doesn't do anything.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-20 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found