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

Re: Re: Tacking a function on to STDOUT?

by RhetTbull (Curate)
on May 28, 2001 at 18:39 UTC ( #83731=note: print w/ replies, xml ) Need Help??


in reply to Re: Tacking a function on to STDOUT?
in thread Tacking a function on to STDOUT?

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.


Comment on Re: Re: Tacking a function on to STDOUT?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://83731]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-08-01 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (256 votes), past polls