[Melbourne-pm] overloading 'print'

Mathew Robertson mathew.robertson at netratings.com.au
Tue Mar 21 15:32:10 PST 2006


Thats an interesting question, and one that I will eventually tackle.   
The whole concept of overriding 'print' itself is fundamentally suspect, 
so I'm specifically trying to *not* allow flexible useage of the package.

This is seperate from the problem that the syntax (as you have specified 
below), is how you would expect to use the module.  Where as I am 
dictating something like:

use MyPrint ...FH...;

package 1;
use MyPrint;
...

package 2;
use MyPrint;
...

which is bad-form on my part...

I will definitely revisit the problem in the future, but for the moment 
it does what I would like.
Mathew

leif.eriksen at hpa.com.au wrote:
> Mathew,
> 	What would happen if I used your code in two different places to
> create two overloaded print handles ? 
>
> package 1;
> ...
> use MyPrint qw(STDERR);
>
> package 2;
> ...
> Use MyPrint qw(STDOUT);
> ...
>
> That "our $this" and "out $filehandle" make me suspicious that the first
> one created would get clobbered. If so, you may need to isolate the tie
> handles from each other some how -  inside the blessed hash springs to
> mind - how to make that public is the next issue - seems you throw it
> away at the moment.
>
>
> Leif
>
> -----Original Message-----
> From: mathew.robertson at netratings.com.au
> [mailto:mathew.robertson at netratings.com.au] 
> Sent: Wednesday, 22 March 2006 9:57 AM
> To: melbourne-pm at pm.org
> Subject: Re: [Melbourne-pm] overloading 'print'
>
> Hi folks,
>
> I asked the question of how to overload 'print' - here are the relevant 
> parts of the implementation, for those that my be interested:
>
> package MyPrint;
> use strict;
> use warnings;
> use Symbol;
> use Exporter;
> use base qw(Exporter);
> our $STDOUT;
> our $filehandle;
> our $print = 1;
> our $println = 1;
> our $this;
>
> # Install a println handler to handle println'ing to a filehandle or
> STDOUT
> sub IO::Handle::println { my $FH = shift; print $FH (@_,$/); }
> sub main::println {  CORE::print (@_,$/); }
>
> sub import {
>   my $class = shift;
>   my $caller = caller;
>   my $sym = gensym;
>   $STDOUT = select unless (defined $STDOUT);
>
>   my %options;
>   get_options('use',\%options, at _) if @_;
>   $filehandle = (@_ > 0) ? shift : (defined $filehandle ? $filehandle : 
> $STDOUT);
>   die "Invalid filehandle specification" unless (defined $filehandle);
>
>   $this = tie *$sym, $class, $filehandle;
>   bless $sym, $class;
>
>   if ($print) { select $sym; }
>   if ($println) {  no strict 'refs';  *{$caller."::println"} = 
> \&{$class."::LM_println"}; }
>
>   return $class;
> }
>
> sub unimport {
>   my $class = shift;
>   my $caller = caller;
>   if ($print) { select $STDOUT; }
>   if ($println) { no strict 'refs';  *{$caller."::println"} = 
> \&{$class."::CORE_println"}; }
>   return $class;
> }
>
> sub TIEHANDLE {
>   my $class = shift;
>   my $self = bless {}, $class;
>   if (@_ > 0) {
>     $self->{fh} = shift or die "No filehandle specified in
> constructor.";
>   } else {
>     $self->{fh} = select;
>   }
>   return $self;
> }
>
> sub PRINT {
>   my $self = shift;
>   my $fh = *{ $self->{fh} };
>   CORE::print $fh (mp(@_));
> }
>
> sub PRINTLN {
>   my $self = shift;
>   my $fh = *{ $self->{fh} };
>   CORE::print $fh (mp(@_).$/);
> }
>
> no warnings 'once';
> *new = *TIEHANDLE;
> *print = *PRINT;
> *println = *PRINTLN;
> use warnings 'once';
>
> sub CORE_println {  CORE::print $STDOUT @_,$/; }
> sub LM_println {  PRINTLN $this, @_; }
>
> sub mp { ...do funky stuff... }
>
> 1;
>
> Thanks for all the suggestions,
> Mathew
> _______________________________________________
> Melbourne-pm mailing list
> Melbourne-pm at pm.org
> http://mail.pm.org/mailman/listinfo/melbourne-pm
> **********************************************************************
> IMPORTANT
> The contents of this e-mail and its attachments are confidential and intended
> solely for the use of the individual or entity to whom they are addressed.  If
> you received this e-mail in error, please notify the HPA Postmaster, postmaster at hpa.com.au,
> then delete  the e-mail.
> This footnote also confirms that this e-mail message has been swept for the
> presence of computer viruses by Ironport. Before opening or using any
> attachments, check them for viruses and defects.
> Our liability is limited to resupplying any affected attachments.
> HPA collects personal information to provide and market our services. For more
> information about use, disclosure and access see our Privacy Policy at
> www.hpa.com.au
> **********************************************************************
>   


More information about the Melbourne-pm mailing list