Create a class in the normal way and use overload to overload the reference to produce the value in a string context:
c:\test>p1 package Enhanced::String; use overload '""' => \&asString; sub new { my $class = shift; return bless { string => shift }, $class; } sub substr{ return substr( $_[0]{ string }, $_[1], $_[2] ) };; sub asString { return $_[ 0 ]{ string } }; package main; my $obj = Enhanced::String->new( 'the string' );; print $obj->asString;; the string print $obj;; the string print $obj->substr( 4, 3 );; str
In reply to Re: variables as classes
by BrowserUk
in thread variables as classes
by nmerriweather
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |