package Foo; use strict; use overload '""' => \&interp; sub new { my $class = shift; bless { str => $_[0], re => qr/^$_[0]$/, }, $class; } sub as_string { $_[0]->{str}; } sub re { $_[0]->{re}; } sub interp { my $self = shift; my $condition = 1; # 1 if called in qr{} context # 0 otherwise $condition ? $self->re : $self->as_string; } 1;