Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

overriding print built-in

by jeremyh (Beadle)
on Apr 12, 2006 at 00:28 UTC ( #542712=perlquestion: print w/ replies, xml ) Need Help??
jeremyh has asked for the wisdom of the Perl Monks concerning the following question:

I am having trouble overriding the "print" builtin.
Here is an example that should override both "open" and "print".
#! perl -w use strict; use subs qw( open print ) ; print 2; open(1,2) or die "$!: open() error"; sub open { CORE::print $_[0] + $_[1], "\n" } sub print { CORE::print $_[0] + 10, "\n" }
Output is

23
instead of
12
3

It looks like "open" is being overridden, but not "print".

Comment on overriding print built-in
Download Code
Re: overriding print built-in
by m.att (Pilgrim) on Apr 12, 2006 at 01:35 UTC
    As per the last line in the section of perlsub entitled 'Overriding Built-in Functions':

    Finally, some built-ins (e.g. "exists" or "grep") can't be overridden.

    The reason being that some builtins cannot be reliably emulated via prototypes. print is one of those functions. You can tell which functions you can indeed override with the prototype function:

    print prototype CORE::print;

    Prints nothing. Also, from perldoc -f prototype:

    If FUNCTION is a string starting with "CORE::", the rest is taken as a name for Perl builtin.  If the builtin is not over-ridable (such as "qw//") or its arguments cannot be expressed by a prototype (such as "system") returns "undef" because the builtin does not really behave like a Perl function.

    The specific issue with print is that you can't express the optional filehandle target. (eg: print FH "foo";)

    Hope that helps.

    m.att

    ** added reasoning behind print not being overrideable.

Re: overriding print built-in
by grantm (Parson) on Apr 12, 2006 at 01:51 UTC

    While you can't override print, you may be able to achieve your desired result using a tie instead.

    For example, Sprog uses this module to translate code like this ...

    print @args;

    ... into code like this ...

    $some_object->print(@args);

      Specifically you're going to be interested in the TIEHANDLE and related routines from perltie; and perhaps IO::Scalar or the like for inspiration.

      Thank you, this looks like it will work for what I want to do, which is to make a custom output formatting function send output from one server to another over an Oracle DB link, without touching the code of the custom output function.

      Thanks again,

      Jeremy

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2014-09-20 04:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (152 votes), past polls