[Melbourne-pm] overloading 'print'

Mathew Robertson mathew.robertson at netratings.com.au
Thu Mar 16 23:19:15 PST 2006


'override' is what I would prefer.

If I overload '""' all kinds of weird stuff can happen, 'depending on 
context...' - but the end-result is not really what I am aiming for 
either... :-(

Mathew

Sisyphus wrote:
> ----- 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
>
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/melbourne-pm/attachments/20060317/3a2673f9/attachment.html


More information about the Melbourne-pm mailing list