Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Overloading '{}' or 'sub'

by pkirsch (Novice)
on Jan 01, 2009 at 19:14 UTC ( [id://733658]=perlquestion: print w/replies, xml ) Need Help??

pkirsch has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm searching for a solution for overloading the closing bracket '}' operator or the 'sub'-statement.

The idea behind this is, that I want to execute code in the scope of the brackets (some kind of a hook). This code needs to be the last operation of the context which the brackets are including. Furthermore every 'sub' (in a package) should be hooked with this post-execution-operation.
Sadly I cannot rely on the return statement, otherwise I would like to overload this statement.
I searched the perlmonks.org for 'overload builtin' but it gives me only hints for overloading arithmetical statements. The perl manpage for overload does not mention overloading 'sub' or '{}'.(In the case that: this way it is not possible, then what about the symbol table?).

I tried e.g. following:
BEGIN { use overload; overload::Method "*{}" => sub { print "Overloaded\n"; }; } sub test () { print "Not Overloaded\n"; }; test();

Regards,

Replies are listed 'Best First'.
Re: Overloading '{}' or 'sub'
by Corion (Patriarch) on Jan 01, 2009 at 19:26 UTC

    Does Hook::LexWrap provide you with enough rope?

    Maybe you can show us more exactly what you want to achieve. There also are Sub::Uplevel and Scope::Upper, which give you access to the variables of the route calling your code. These all provide you with lots of rope to hang yourself, in other words, I'm not exactly sure that what you want to achieve should be done through this.

    The easiest way to "append" code to a subroutine is to just replace the subroutine with your own:

    package Somewhere; sub test { print "leaving original test\n"; }; package My::Somewhere; use vars qw($old_test); $old_test = &Somewhere::test; *Somewhere::test = sub { $old_test->(@_); print "leaving overridden test\n"; };

    But if you're trying to use overload, maybe you're dealing with objects? Then, the best way could maybe just be to subclass your target class instead of hacking its code?

Re: Overloading '{}' or 'sub'
by jasonk (Parson) on Jan 01, 2009 at 20:06 UTC

    See Scope::Guard, which is designed to do exactly that, you create it with a subroutine to run when it goes out of scope...

    use strict; use warnings; use Scope::Guard; sub foo { my $blah = shift; my $sg = Scope::Guard->new( sub { print "This runs at the end of the scope\n"; } ); print "Doing some other stuff now...\n"; }

    www.jasonkohles.com
    We're not surrounded, we're in a target-rich environment!
      Wasn't operator overloading designed by Satan for evil C++ programmers to torture each other in the hell that they have created for themselves. Or something?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2024-04-26 05:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found