in reply to variables as classes
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: variables as classes
by nmerriweather (Friar) on Sep 04, 2006 at 06:07 UTC |