Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Possible to get coderef of CORE::print to alias it?

by Yary (Pilgrim)
on Feb 19, 2013 at 00:04 UTC ( [id://1019419]=perlquestion: print w/replies, xml ) Need Help??

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

I have a function that I might want to just be an alias to "print", or I might want it to be something else. This is what I tried:
my $pick_real_print = 1; use vars qw(&printish); *printish = $pick_real_print ? *CORE::print : sub { print "I'm not really print!\n" }; CORE::print "Here is the real deal\n"; printish "printing\n";
which prints:
Here is the real deal
Undefined subroutine &CORE::print called at - line 7.
I know print is a very special function... is it possible to alias to it?

ps. I already have the workaround sub { print } in place. I was hoping to benchmark the difference between that and aliasing via code reference, since this will be in a tight loop, and I'm curious.

Replies are listed 'Best First'.
Re: Possible to get coderef of CORE::print to alias it?
by LanX (Saint) on Feb 19, 2013 at 00:33 UTC
    > is it possible to alias to it?

    Nope!

    DB<128> $CORE::{substr} => *CORE::substr DB<129> $CORE::{print} => undef

    It's not really as a "function" in CORE namespace, to rule out overriding.

    prototype FUNCTION Returns the prototype of a function as a string (or "un +def" if the function has no prototype). FUNCTION is a referenc +e to, or the name of, the function whose prototype you want to r +etrieve. If FUNCTION is a string starting with "CORE::", the res +t is taken as a name for Perl builtin. If the builtin is no +t overridable (such as "qw//") or if its arguments cannot + be adequately expressed by a prototype (such as "system"), prototype() returns "undef", because the builtin does n +ot really behave like a Perl function. Otherwise, the str +ing describing the equivalent prototype is returned.

    DB<131> prototype 'CORE::print' => undef DB<132> prototype 'CORE::substr' => "\$\$;\$\$"

    Nevertheless can it be called.

    DB<133> CORE::print("bla") bla

    > I know print is a very special function...

    Thats the point, print is magic about indirect object syntax:

    eg print STDERR "Fatal!"

    This behavior can't be mimiced with normal prototypes, so no STASH entry.

    Cheers Rolf

      Thanks, knew I'd seen that somewhere, forgot about "prototype".

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-23 13:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found