in reply to Using overload. Any complete (simple) examples?

Here is a simple Message object We use in our office: It stringifies into a simple message, or as a number will give a code, and evaluates as true or false as dictated by the user. It also allows you to push on some extra data with push, for debuging or general extra information on a message.

example of use:
#Message->new(<Number>,<MSG>,<BOOL>);
Message->new(12345,'This is my Message or error message',0);

package Message; use strict; use overload '""' => 'str', '0+' => 'num', 'bool' => 'bool', fallback +=> 1; ###################################################################### +######### sub new { my $class = shift; my $num = shift; my $str = shift; my $bool = shift; my $data = {num => $num, str => $str, bool => $bool, data => [], }; if ( ! defined $data->{bool} ) { $data->{bool} = ( $data->{num} ? 1 : 0 ); } bless $data, $class; if ( @_ ) { if ( ref $_[0] eq 'ARRAY' ) { foreach my $element (@_) { $data->push($element); } } else { $data->push(@_); } } return $data; } ###################################################################### +######### sub push { my $self = shift; my $obj = shift; push @{$self->{data}},$obj; } ###################################################################### +######### sub num { my $self = shift; return $self->{num}; } ###################################################################### +######### sub str { my $self = shift; return $self->{str}; } ###################################################################### +######### sub bool { my $self = shift; return $self->{bool}; } ###################################################################### +######### 1;