Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Overriding CORE::GLOBAL::bless

by rinceWind (Monsignor)
on Aug 19, 2005 at 11:01 UTC ( [id://485061]=perlquestion: print w/replies, xml ) Need Help??

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

I've decided to work on my Devel::Leak::Object, as I need to do some leak tracing in a Class::DBI app.

To date, I've only been able to use it in cases like this manually tracking objects, as setting CORE::GLOBAL::bless during BEGIN interferes with initialisation of several things including the debugger. I had an idea that we could set CORE::GLOBAL::bless in a callable sub, thus:

sub track_blessings { my $type = shift; if ($type =~ /^global$/i) { *CORE::GLOBAL::bless = \&bless; } }

The trouble is, that this does not seem to work. The original technique of setting it inside an import routine works fine (see the source code for Devel-Leak-Object-0.02 on CPAN, in particular test t/004_auto.t). I've been staring at my track_blessings routine, and I can't see a reason why this override is not happening. Maybe I need another cup of coffee.

--

Oh Lord, won’t you burn me a Knoppix CD ?
My friends all rate Windows, I must disagree.
Your powers of persuasion will set them all free,
So oh Lord, won’t you burn me a Knoppix CD ?
(Missquoting Janis Joplin)

Replies are listed 'Best First'.
Re: Overriding CORE::GLOBAL::bless
by sh1tn (Priest) on Aug 19, 2005 at 12:23 UTC
    You may want to use subs pragma for overriding:
    use subs qw(bless);
    Update: In fact you have to use module or subs pragma in order ro override in CORE::GLOBAL.


      Added use subs qw(bless); this has made no difference whatsoever. The track_blessings sub is performing the typeglob assignment to CORE::GLOBAL::bless, but Devel::Leak::Object::bless is not getting called.

      --

      Oh Lord, won’t you burn me a Knoppix CD ?
      My friends all rate Windows, I must disagree.
      Your powers of persuasion will set them all free,
      So oh Lord, won’t you burn me a Knoppix CD ?
      (Missquoting Janis Joplin)

Re: Overriding CORE::GLOBAL::bless
by chromatic (Archbishop) on Aug 19, 2005 at 17:08 UTC

    This is tricky. I've found that using the subs pragma in the package space of the package in which you want to override an operator before you load the module normally seems to work:

    use Test::More tests => 2; package CGI; use subs 'bless'; package main; my $called = 0; use CGI; sub CGI::bless { $called++; CORE::bless( $_[0], $_[1] ); }; my $foo = CGI->new(); is( $called, 1, 'bless() should call overridden version' ); isa_ok( $foo, 'CGI', 'which should return something that' );
Re: Overriding CORE::GLOBAL::bless
by dragonchild (Archbishop) on Aug 19, 2005 at 17:34 UTC
    Are you positive bless is overiddable or is it like print?

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      The point is that I am successfully doing this from an importer sub, as per Devel::Leak::Object 0.02 - and it gets called. But I want to achieve the same thing later on under user control, after BEGIN has finished.

      --

      Oh Lord, won’t you burn me a Knoppix CD ?
      My friends all rate Windows, I must disagree.
      Your powers of persuasion will set them all free,
      So oh Lord, won’t you burn me a Knoppix CD ?
      (Missquoting Janis Joplin)

Re: Overriding CORE::GLOBAL::bless
by fro (Novice) on Jan 31, 2006 at 12:13 UTC
    > I've been staring at my track_blessings routine, and I can't see a reason why this override is not happening.
    That's because you cannot override built-in functions
    during runtime - when perl is _compiling_ this line:

    bless $foo, "Bar";

    it decides what op code to generate (in that case what function to
    invoke). This happens at compile time. After that, when
    you call your track_blessings() it is too late - the
    calling of CORE::bless is already compilied and hardcoded
    in the syntax opcode tree.

      Yup, that's what I found. I didn't post my conclusion here, but to my use.perl journal.

      --

      Oh Lord, won’t you burn me a Knoppix CD ?
      My friends all rate Windows, I must disagree.
      Your powers of persuasion will set them all free,
      So oh Lord, won’t you burn me a Knoppix CD ?
      (Missquoting Janis Joplin)

Log In?
Username:
Password:

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

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

    No recent polls found