Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re (tilly) 1: overloading the print function (or alternatives)

by tilly (Archbishop)
on Dec 04, 2000 at 10:07 UTC ( [id://44747]=note: print w/replies, xml ) Need Help??


in reply to overloading the print function (or alternatives)

I think that this hack is ugly. Not only in that it is unaesthetic, but in this elementary error lies a good lesson that you would deprive people of. Getting around misunderstandings with magic tricks both keeps people from learning how things really work, but also makes it harder for them to later on figure out what you did.

But look at perlsub for the section on overriding built-in global functions. (You can do this per package or for all packages at once.)

Alternately you can look at perltie and create yourself a tied filehandle that you select before calling their code.

Personally rather than making this work as they want, I would select a tied filehandle that would take print's and cough and die with an informative error. Then people could still print to STDERR for debugging output.

  • Comment on Re (tilly) 1: overloading the print function (or alternatives)

Replies are listed 'Best First'.
Re: Re (tilly) 1: overloading the print function (or alternatives)
by Hot Pastrami (Monk) on Dec 04, 2000 at 23:42 UTC
    How about overloading it in a more informative way? Perhaps you could overload print() with something like this (untested):

    sub print { my $string = join '', @_; if ($string =~ /^(Content-type:)/) { CORE::print $string; } else { CORE::print "Content-type: text/html\n\n"; die "You used print() rather than returning a string, you naughty +person"; } }
    Something I DON'T know, however... would one need to prototype this overloaded print() to assure that it would work when used without parenthases, or is that taken care of becuase the built-in print() is already prototyped? You'd probably want to be sure beore doing it this way.

    Alan "Hot Pastrami" Bellows
      Then you would mess up someone who had inserted:
      print STDERR "Called node 'foo'\n";
      as a debugging aid. (The messsage should show up in your webserver's logs.) The following (untested) code is much nicer:
      package noprint; use Carp; sub PRINT { my $msg = Carp::longmess("You must return rather than print"); print "Content-type: text/plain\n\n$msg"; die $msg; } *PRINTF = *PRINT; sub TIEHANDLE { return bless ({}, shift); }
      and elsewhere in the code:
      tie(*NOPRINT, 'noprint'); select(NOPRINT); # time passes while the page is built. # Before spitting out the final page: select(STDOUT);
      That catches the newbie error. Without the potential for headaches that overriding print causes.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2025-07-12 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.