[Melbourne-pm] overloading 'print'

Sisyphus sisyphus1 at optusnet.com.au
Thu Mar 16 22:43:01 PST 2006


----- Original Message ----- 
From: "Mathew Robertson" <mathew.robertson at netratings.com.au>
To: "Perl Mongers" <melbourne-pm at pm.org>
Sent: Friday, March 17, 2006 4:33 PM
Subject: [Melbourne-pm] overloading 'print'


> Hi folks,
> 
> I have a situation where I would like to overload the 'print' and 
> 'printf' functions, so that I can do some translation stuff in them. 

If you really want to overload, rather than override then something like:

package Foo;
use warnings;

use overload '""'   => \&overload_print;

$z = Foo->new(8);

print $z, "\n";


sub new {
    my ($class, $val) = @_;
    my $self = {value => $val};
    bless $self, $class;
    return $self;
}

sub overload_print {
    my $ret = 'The value I want is ';
    my $p = $_[0]->{value};
    $ret .= $p * 7;
}

__END__

Not sure if that's what you want to do.

Cheers,
Rob



More information about the Melbourne-pm mailing list