Certain perl built-ins can be overriden and others can't. For a discussion on this, see this node. The following code shows one way to override a built-in:
use warnings; use subs 'die'; #our own print, but it doesn't override the built-in sub print { CORE::print scalar(localtime),": "; CORE::print(@_); } #our own die, will override the built-in sub die { print "goodbye cruel world\n";} #calls our custom print #notice you have to use &print() notation &print( "hello world\n"); #calls built in print print "goodbye\n"; #calls our overriden die die "hello";
This outputs:
Mon May 28 10:41:21 2001: hello world goodbye goodbye cruel world
You could also use CORE::GLOBAL to override the built-in everywhere (for example if you wanted to add your own custom die handler to catch die in other modules). Here's a package that exports it's own die sub that will be used everywhere -- use with caution!
#mydie.pm #a package to export a global die, overriding the perl builtin #reference perldoc perlsub package mydie; require Exporter; use vars qw/@ISA @EXPORT/; @ISA = 'Exporter'; @EXPORT_OK = qw/die/; sub import { my $pkg = shift; return unless @_; my $sym = shift; my $where = ($sym =~ s/^GLOBAL_// ? 'CORE::GLOBAL' : caller(0)); $pkg->export($where, $sym, @_); Exporter::import($pkg,@_); } sub die { print "goodbye cruel world\n"; CORE::die $_[0]; }
and here's an example that uses the globally overriden die:
#!/usr/local/bin/perl use warnings; use strict; use mydie qw(die); print "hello world\n"; #calls the die exported by mydie.pm die "argh!!!";
prints:
hello world goodbye cruel world argh!!! at mydie.pm line 20.

In reply to Re: Re: Tacking a function on to STDOUT? by RhetTbull
in thread Tacking a function on to STDOUT? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.