Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Overriding built-ins

by chas (Priest)
on Jun 01, 2005 at 14:26 UTC ( [id://462592]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

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

I thought I could override a built-in by using the subs pragma as in:
use strict; use warnings; use subs qw(print); sub print {CORE::print "OK, $_[0]\n";} print "bla";

However, the result is just to print "bla". I wonder why the print function wasn't overridden. (BTW, if I replace the 1st, 2nd, and 4th occurrences of "print" with "shift", i.e. everywhere except in "CORE::print", then the shift function is overridden.) Thanks for any comments!
chas

Replies are listed 'Best First'.
Re: Overriding built-ins
by tlm (Prior) on Jun 01, 2005 at 14:29 UTC

    You may find this node useful.

    The short answer is that print is one of a handful of builtins that cannot be overridden. See in particular diotalevi's comments in the thread referred to above.

    the lowliest monk

      Thanks tlm (and everyone else)! I should have searched perlmonks before asking (I did look in the docs but didn't find the answer.) The print function is an obvious choice to try out overriding since it would be easy to see the effect; presumably one wouldn't actually want to do this (for other than experimentation), though.
      chas
Re: Overriding built-ins
by dragonchild (Archbishop) on Jun 01, 2005 at 14:30 UTC
    print is one of those non-overridable built-ins. There's about a half-dozen or so that can't be overriden. *shrugs*

    • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
    • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
Re: Overriding built-ins
by Roy Johnson (Monsignor) on Jun 01, 2005 at 14:32 UTC
Re: Overriding built-ins
by ikegami (Patriarch) on Jun 01, 2005 at 14:32 UTC

    Not all core functions can be overridden. print is one of those that cannot.

    Even the following doesn't work

    package MyPrint; sub print { shift; CORE::print("[\n"); CORE::print(@_); CORE::print("]\n"); } package main; bless *STDOUT{IO}, 'MyPrint'; print("Hello World!\n"); # builtin print STDOUT ("Hello World!\n"); # builtin

    but this does

    STDOUT->print("Hello World!\n"); # custom

    I think tieing also works.

Re: Overriding built-ins
by Joost (Canon) on Jun 01, 2005 at 14:32 UTC
Re: Overriding built-ins
by ysth (Canon) on Jun 01, 2005 at 14:39 UTC
    Usually you want to use the same prototype for an overriding function as the builtin has. If prototype("CORE::foo") is not defined, foo is usually not overridable (though require is an exception).
Re: Overriding built-ins
by diotalevi (Canon) on Jun 02, 2005 at 00:50 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://462592]
Approved by Joost
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.