die alias

Discussion in 'Perl Misc' started by George Bouras, Mar 21, 2013.

  1. I want to to have my die, the following does not work


    use strict;
    use warnings;
    sub die;

    die "oups";

    sub die {
    print "There was a problem : $_[0]\n";
    }
     
    George Bouras, Mar 21, 2013
    #1
    1. Advertisements

  2. "Does not work" is about the worst problem description imaginable.
    Do you also walk into a garage and say just "My car doesn't work" and
    expect the mechanic to fix whatever problem there is without even
    telling him what the problem is?

    You provided code, well, that's one step in the right direction.

    But you should always also describe what you expected the code to do and
    what behaviour you observed instead.

    Without that the mechanic may fix the rattling ash tray without ever
    noticing the leaking radiator simply because you didn't tell him about
    it.

    jue
     
    Jürgen Exner, Mar 21, 2013
    #2
    1. Advertisements

  3. George Bouras

    J. Gleixner Guest

    You might find this useful:
    http://www.effectiveperlprogramming.com/blog/1310
     
    J. Gleixner, Mar 21, 2013
    #3
  4. George Bouras

    Justin C Guest

    BEGIN {
    *CORE::GLOBAL::die = sub {
    print "There was a problem : $_[0]\n";
    }

    But is it a good idea?


    Justin.
     
    Justin C, Mar 21, 2013
    #4
  5. The short answer is

    *CORE::GLOBAL::die = sub { print "There was a problem : $_[0]\n"; }

    more details are in the CORE manpage ('perldoc CORE').
     
    Rainer Weikusat, Mar 21, 2013
    #5
  6. BEGIN {
    }
     
    Rainer Weikusat, Mar 21, 2013
    #6
  7. George Bouras

    C.DeRykus Guest

    Does "not work" refer to the warning:

    Ambiguous call resolved as CORE::die(), qualify as such or use & at die.pl line 8.
    oops at die.pl line 8.

    Add 'use diagnostics' for further info.
     
    C.DeRykus, Mar 21, 2013
    #7
  8. I must also a print alias; the same method does not work.


    #!/usr/bin/perl
    use strict;
    use warnings;

    BEGIN {
    *CORE::GLOBAL::die = \&error;
    *CORE::GLOBAL::print = \&NotifyAll
    };

    die "oups";

    sub error { print "There was a problem : $_[0]" }
    sub NotifyAll { CORE::print ">>$_[0]<<"; }
     
    George Mpouras, Mar 21, 2013
    #8
  9. There is no other way. It is a apache xml interface between different
    systems , where some existing 3rd party code must wraped and run as it is to
    send back messages
     
    George Mpouras, Mar 21, 2013
    #9
  10. BEGIN { *CORE::GLOBAL::die = sub { ... } };

    is great, can we make it work with print also ?
     
    George Mpouras, Mar 21, 2013
    #10
  11. George Bouras

    Keith Keller Guest

    Whenever I think this is true, I usually stop for a moment and try to
    figure out whether it's actually true. It almost always ends up false.

    In this case, did you look into $SIG{__DIE__} as others have suggested?
    perldoc -f die and perldoc perlvar (search for __DIE__) for details.

    --keith
     
    Keith Keller, Mar 21, 2013
    #11
  12. perldoc -f die and perldoc perlvar (search for __DIE__) for details.

    die is solved . Now I have to solve the print alias
     
    George Mpouras, Mar 21, 2013
    #12
  13. dead end here
     
    George Mpouras, Mar 21, 2013
    #13
  14. George Bouras

    J. Gleixner Guest

    How many times are you going to post that?? Once is enough...


    http://lmgtfy.com/?q=perl+override+print

    I didn't really read about Devel::UncheckedOps because I'd never go that
    route.

    But seriously, it's going way beyond *normal* to go this route. Maybe a
    search/replace of 'print' to 'my_print' or something simple like that
    is enough.
     
    J. Gleixner, Mar 21, 2013
    #14
  15. Óôéò 22/3/2013 01:39, ï/ç J. Gleixner Ýãñáøå:

    # the following is doing the job



    package IO::Override;
    use strict;
    use warnings;
    use base qw<Tie::Handle>;
    use Symbol qw<geniosym>;

    sub TIEHANDLE { return bless geniosym, __PACKAGE__ }
    tie *PRINTOUT, 'IO::Override';
    our $OLD_STDOUT = select( *PRINTOUT );

    sub PRINT
    {
    no strict;
    shift;

    my $var = $_[0] +1 ;

    CORE::print $OLD_STDOUT ">>$var<<\n";
    }



    print 99;
     
    George Mpouras, Mar 22, 2013
    #15
  16. #simpler ,but there is no reason any more, the "remote" guy agreed to
    use a printcustom




    use strict;
    use warnings;

    tie *PRINTOUT, 'main';
    sub TIEHANDLE { return bless {}, 'main' }

    my $NORMALPRINT = select( *PRINTOUT );


    sub PRINT
    {
    shift;
    no strict;
    print $NORMALPRINT ">>$_[0]<<\n";
    }

    print 'hello';
     
    George Mpouras, Mar 22, 2013
    #16
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.