Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^4: Overloading different instances differently. (Symbol::delete_package)

by lodin (Hermit)
on Feb 23, 2008 at 15:51 UTC ( [id://669753]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Overloading different instances differently.
in thread Overloading different instances differently.

The long-time standard module Symbol to the rescue. Just change undef %Foo::; to delete_package('Foo');.

#! /usr/bin/perl -w use strict; use Symbol 'delete_package'; my $s; print "Creating functions\n"; my $x = TellDestroy->new(sub {print $s}, "variable sub"); *Foo::function = TellDestroy->new(sub {print $s}, "glob sub"); print "Clearing variable\n"; undef $x; print "Clearing glob\n"; delete_package('Foo'); print "Exiting\n"; package TellDestroy; use Scalar::Util qw(refaddr); my %function_name; sub new { my ($class, $self, $name) = @_; $function_name{refaddr($self)} = $name; bless $self, $class; } sub DESTROY { my $self = shift; my $name = delete $function_name{ refaddr($self) }; print STDERR "$name is going away\n"; } __END__ Name "Foo::function" used only once: possible typo at ... Creating functions Clearing variable variable sub is going away Clearing glob glob sub is going away Exiting

lodin

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-25 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found