Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

perl calls die() as a method

by kappa (Chaplain)
on Aug 18, 2005 at 14:08 UTC ( [id://484783]=perlquestion: print w/replies, xml ) Need Help??

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

Good day, fellow monks!

Yes, perl does consider my die an indirect object method call. I checked with B::Deparse. And the question is -- how to prevent the situation?

My code (I tried to implement something like My::Exception class from this perl.apache.org doc) is below.

File AA/BB.pm

package AA::BB; sub die(@) { die @_ }; sub import { *CORE::GLOBAL::die = \¨ } 1;
File testdie.pl:
#! /usr/bin/perl -w use strict; use AA::BB; eval { die AA::BB->new; };
Deparsed test.pl:
% perl -MO=Deparse testdie.pl eval { 'AA::BB'->CORE::GLOBAL::die->new; };

I'd really like to throw exceptions without additional parens like this: die MyApp::Error->new() and this very syntax is everywhere on the web.

Ah, and this is perl 5.6.2.

Update: looks like a bug fixed somewhere between 5.6.2 and 5.8.7. So the new question is: is there a way to force die() as a sub? Upgrading perl is not an option for me due to different Unicode :(

--kap

Replies are listed 'Best First'.
Re: perl calls die() as a method
by Joost (Canon) on Aug 18, 2005 at 14:37 UTC
    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).

      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".  :-)

      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
Re: perl calls die() as a method
by chromatic (Archbishop) on Aug 18, 2005 at 16:52 UTC
    So the new question is: is there a way to force die() as a sub?

    You can always parenthesize to disambiguate for the parser.

Log In?
Username:
Password:

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

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

    No recent polls found