#!/usr/bin/perl use strict; use warnings; package Ovl; use overload '""' => \&mystring; sub new { my $ret = bless {}, shift(); $ret->{_name} = shift; $ret->{_str_code} = \&_str_default; return $ret; } sub _str_default { my $self = shift; "!" . $self->{_name} . "!" } sub mystring { my $self = shift; my $str_code = $self->{_str_code} || \&_str_default; $self->$str_code( @_ ); } package main; my $o = Ovl->new( "default" ); print "$o\n"; my $o_undef = Ovl->new( undef ); $o_undef->{_str_code} = sub { "I R UNDEFINED" }; print "$o_undef\n" if $o_undef; my $o_numeric = Ovl->new( 15 ); $o_numeric->{_str_code} = sub { sprintf( "%0.5f", shift->{_name} ) }; print "$o_numeric\n" if int( $o_numeric ) == 15; exit 0; __END__