Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

variables as classes

by nmerriweather (Friar)
on Sep 04, 2006 at 04:51 UTC ( [id://570999]=perlquestion: print w/replies, xml ) Need Help??

nmerriweather has asked for the wisdom of the Perl Monks concerning the following question:

python ( and some other languages ) can do some crazy stuff where a simple string variable is a class other than a string. ie, $email is an Email class object, when set it can only hold an email or toss an error. when you call print, or do any sort of string operation to it, it behaves like a string because of a representation function

i kind of really need something like that in perl - in one webapp, I'm regexing a single variable to see if it's a url/email 15x , tossing it through various functions that are different from one another. i'd love to replace the tests with a simple class check, and just change the instantiation to use a class instead of a string.

I've been through all my perl books , the pod, and perlmonks, but I can't seem to find anyone doing behavior like this. does anyone have a clue? can this even be done?

Replies are listed 'Best First'.
Re: variables as classes
by BrowserUk (Patriarch) on Sep 04, 2006 at 05:41 UTC

    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.
      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; }
Re: variables as classes
by imp (Priest) on Sep 04, 2006 at 05:45 UTC
    While it would be possible to create a class to encapsulate the behaviour, or a tied variable that forbids storage of non-email addresses.. I don't think it is particularly wise to do so.

    Why not just validate the user input early on, and send them back to the form entry if they do not provide an email address? I am fond of using Data::FormValidator and HTML::FillInForm for this. If you use CGI::Application then CGI::Application::Plugin::ValidateRM encapsulates this behaviour for you quite nicely.

      I already do that for user submitted data.

      I need this functionality for data that is coming out of my database, that I perform various markup or logic operations on. If I pull an email address out of the DB, all of my various functions still check it to verfiy that it is an email when they're passed it.

      I don't want to accidentally send an email to a non-email, or turn a non-http address into an href. Those functions need to catch errors, and every function I write checks the input data.

      With some custom classes, I can just check the ISA before running a regex. thats way more efficient than running a regex over and over again. And if I'm not using a class, but a string, then I just run the regex.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://570999]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2024-04-23 16:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found