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

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: variables as classes
by nmerriweather (Friar) on Sep 04, 2006 at 06:07 UTC
    thanks a ton! that did it
    package Email; use overload '""'=> \&ADDRESS , fallback=> 1; my $RE_EMAIL= qr/^[\w\-\+\._]+\@[a-zA-Z0-9][-a-zA-Z0-9\.]*\.[a-zA- +Z]+$/ ; sub new { my $proto= $_[0]; my $class= ref($proto) || $proto; my $self= bless( {} , $class ); $self->{ADDRESS}= undef; $self->ADDRESS( $_[1] ); return $self; } sub ADDRESS { my $self= shift; if ( @_ ) { my $email= shift; $email=~ $RE_EMAIL or die "Not a valid email address"; $self->{ADDRESS}= $email; } return $self->{ADDRESS}; } sub _as_str { my ( $self )= @_; return $self->ADDRESS; }