Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I was all set to give a superb answer to Tests for 'printing' code. I went to test my code and ruined it all.

Here's the great code failure:

use Errno ':POSIX'; { no warnings 'redefine', 'once'; local *CORE::GLOBAL::print = sub { $! = ENOSPC; return; }; print 'test', $/ or die $!; }
I intended to override builtin print so that it prints nothing, and triggers a 'No space left on device' system error in the same way print would. The handiness for testing is obvious. Trouble is this code doggedly prints 'test', completely ignoring the override.

After poking around for syntactos and scratching my head and reading the wrong docs, I posed the question in CB. Why doesn't this work? I got two answers.

One was that print is not overridable because it is an object method and doesn't really act like a function. That makes sense - the indirect object notation of print FOO @List makes the handle look very like an object. But, in Perl, or in OO in general, what kind of object method isn't overridable? Why isn't there something like a *CORE::GLOBAL::IO namespace with an overridable print method? Or is there?

The more convincing answer was that print is not overridable because the prototype is too complicated. The ability to have a default indirect object is the only complication I see, but it seems enough. The comment in perldoc -f prototype about things not overridable, things whose arguments are not expressible as a prototype, and things which don't really act like functions was the major justification for this view.

That doesn't entirely hold water. The expression prototype 'CORE::glob' evaluates to undef. Code from Camel3, p307 shows how to make glob take perl regular expressions rather than shell glob patterns. Corrected, it reads,

*CORE::GLOBAL::glob = sub { my $pat = shift; my @got; local *D; if (opendir D, '.') { @got = grep /$pat/, readdir D; closedir D; } return @got; }; package Whatever; print <[a-z_]+\.pm>; #show all pragmas in the current directory
which works just fine. In spite of the prototype docs, glob is overridable, certainly function-like, and it obviously has the simple prototype (;$), though that seems not reflected in CORE::.

I'm puzzled that global print is not overridable. It seems a natural function to want to do that to. It doesn't modify its arguments, so prototypes don't play an important role. In fact, many uses would ignore the arguments. Does anyone know why *CORE::GLOBAL::print is read-only?

I've tried this with my oldest perl, 5.004_02 for djgpp, and the behavior is the same. Prototypes were present in that version, so perhaps that's no surprise. Was global print ever overridable?

I'd appreciate any technical or historical insight the monks may offer on these questions. Print seems an odd function to leave out of the fun.

After Compline,
Zaxo


In reply to !Overriding Builtin print by Zaxo

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (1)
    As of 2014-10-25 04:19 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (141 votes), past polls