Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

(Ovid warning: bad code ahead) Re: Callback Design

by Ovid (Cardinal)
on Jan 16, 2001 at 06:08 UTC ( [id://52133]=note: print w/replies, xml ) Need Help??


in reply to Callback Design

Warning: Bad code ahead!

Just messing around (free time at work). I came up with the following, but I do NOT recommend it :)
my $coderef = sub { my ($package) = caller; $package->global }; package Base; sub AUTOLOAD { no strict 'refs'; # Please note the complete lack of # Sanity checking here :) return ${ $AUTOLOAD } if $AUTOLOAD =~ /^.*::\w+$/; } package Foo; @ISA=('Base'); $global = 'in Foo'; print &$coderef; package Bar; @ISA=('Base'); $global = 'in Bar'; print &$coderef;
Like I said, it's just me messing around. It would be an interesting way to getting to package globals, but it's more of me just playing around rather than offering it as a serious programming solution. It is BAD CODE.

And yes, the dot star is there on purpose. It'll catch package names like Foo::bar::baz.

Overall, this sort of problem is reminiscent of the "variable variable" problem as is indicative of underlying design issues that should be addressed. Be very careful of treading on this thin ice.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Log In?
Username:
Password:

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

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

    No recent polls found