in reply to perl calls die() as a method

Please note that the below is pretty much wrong - it appears that this is a bug in older perls

Deparse is often wrong in corner-cases, including this one:

My perl's deparse does better (v5.8.6) (updated to reflect code below):

BEGIN { $^W = 1; } use AA::BB; use strict 'refs'; print "Caught exception $@" unless eval { do { &CORE::GLOBAL::die('AA::BB'->new) } }; test.pl syntax OK

You're better off trying to run the code in any case: yours doesn't work because there is no AA::BB::new method.

update:

#! /usr/bin/perl -w use strict; use AA::BB; eval { die AA::BB->new; } or print "Caught exception $@";
package AA::BB; sub die(@) { print "die called with args '@_'" }; sub import { *CORE::GLOBAL::die = \¨ } sub new { return bless {},shift; } 1;
Output:
die called with args 'AA::BB=HASH(0x81761c8)'
update2 in other words, your code is allright except for the missing new() method, and deparse is wrong. You should do more checking in your code and try not to depend Deparse.

Also, overriding CORE::GLOBAL::die is completely evil: it can easily break exceptions (as my modified code shows).

Replies are listed 'Best First'.
Re^2: perl calls die() as a method
by chibiryuu (Beadle) on Aug 18, 2005 at 14:57 UTC

    Here, 5.6.1's Deparse shows AA::BB::CORE::GLOBAL::die->new and 5.8.7's shows die('AA::BB'->new).  And it's accurate; 5.6.1 runs die first and 5.8.7 runs new first.

    So the answer to your question would be "upgrade Perl".  :-)

Re^2: perl calls die() as a method
by kappa (Chaplain) on Aug 18, 2005 at 15:11 UTC
    In actual code there's a new() and other methods too. I simplified it before posting here. And I started to Deparse when noticed wrong behaviour. Anyway, looks like chibiryuu is right and it is 5.6.2 bug. In my 5.8.7 both B::Deparse and actual test-suite show that everything is right.

    Now to your remark. I override CORE::GLOBAL::die to FIX exceptions not break. My real die looks like this:

    sub die (@) { unless (ref $_[0]) { CORE::die My::Error->UnCaught(text => join('', @_)); } CORE::die $_[0]; }
    Now, all plain string die-s magically start to raise blessed objects (with a stringify op not to break something).
    --kap