<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<br>
<blockquote cite="mid441A4F18.7050009@perltraining.com.au" type="cite">
  <blockquote type="cite">
    <pre wrap="">Does anone know how to overload 'print'?
    </pre>
  </blockquote>
  <pre wrap=""><!---->
The short answer is that you can't.  Print isn't one of Perl's overloadable
functions.  (The reason is that the syntax is special cased. You cannot make a
user function with the same prototype.)
  </pre>
</blockquote>
ok - I'm guessing this is because print can take a filehandle as the
first argument ?
<blockquote cite="mid441A4F18.7050009@perltraining.com.au" type="cite">
  <pre wrap="">
The longer answer is that you can... if you use IO::File and its print method.
Then you can just subclass IO::File, and mask the print method to do what you need.

The other non-overridable functions are listed in receipt 12.11 in the Perl
Cookbook (1st Edition).  Basically, if the keyword function in toke.c returns a
negative number, then that function can't be overridden.
  </pre>
</blockquote>
ok - thats a bit complicated.&nbsp; Any ideas on the implementation of the
sub-class of IO::File ?<br>
<br>
<br>
<br>
ASIDE:&nbsp; The reason for wanting to override 'print' is...&nbsp; I have built
a language translation module (Locale::MakePhrase) which works quite
well - now I am working on simplifying its usage so that it can be used
within applications more easily. eg:<br>
<br>
Currently if I want to print out a string translated (to say Chinese),
I would code something like:<br>
<br>
&nbsp; use Locale::MakePhrase;<br>
&nbsp; my $mp = Locale::MakePhrase-&gt;new(language =&gt; 'zn_ch', ....);<br>
&nbsp; .....<br>
&nbsp; print $mp-&gt;translate("Some text to translate");<br>
<br>
thus to use the module within your application, would require the
application code to modified to support the L::M syntax.&nbsp; What I would
like to do is extend L::M to override 'print' so that the code becomes
much simpler:<br>
<br>
&nbsp; use Locale::MakePhrase qw(print);<br>
&nbsp; Locale::MakePhrase-&gt;new(language&nbsp; =&gt; 'zn_ch',...);<br>
&nbsp; ...<br>
&nbsp; print "Some text to translate";<br>
<br>
Thus with two simple lines at the top of the application, you
automagically get all of the translation facilities.&nbsp; If you choose to
use a different translation tool, you simply change those two lines -
thus saving you from having to re-code your app for the new API.<br>
<br>
regards,<br>
Mathew<br>
</body>
</html>