Look at the overload pragma. I don't think you can overload the scalar context, but try overloading the structure (which is denoted by "" , which you must quote to become silly looking '""' , citing the use of the q operator, make it more readable).
#!/usr/bin/env perl use strict; use warnings; package MyObject; use Moose; use overload q("") => sub { return shift->val() }; has 'val' => ( isa => 'Str', is => 'rw', required => 1); package main; my $obj = MyObject->new( val => 'Hello' ); print $obj;
source share