#!/usr/bin/env perl use warnings; use strict; { package StringThing; use Scalar::Util qw/blessed/; use Carp; use overload fallback => 0, '""' => \&str_warn, '.' => \&concat, '.=' => \&concat; sub new { my ($class, $self) = @_; return $self if blessed($self) && $self->isa(__PACKAGE__); return bless \"$self", ref($class)||$class; } sub str_warn { carp "unintended stringification"; goto &str; } sub str { return ${+shift} } sub concat { my ($self,$other,$swap) = @_; $other=$$other if blessed($other) && $other->isa(__PACKAGE__); return $self->new( $swap ? $other.$$self : $$self.$other ); } } my $x = StringThing->new("foo"); my $y = StringThing->new("bar"); my $z = "$x $y"; print $z->str, "\n"; my $zz; $zz = "$x $y"; # warns print $zz->str, "\n"; # dies __END__ foo bar unintended stringification at test.pl line 34. Can't locate object method "str" via package "foo bar" (perhaps you forgot to load "foo bar"?) at test.pl line 35.