Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Substituting method from object

by Angel (Friar)
on Dec 03, 2002 at 01:08 UTC ( [id://217097]=perlquestion: print w/replies, xml ) Need Help??

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

I have an object. It does validation and data storage acting as a wrapper with a database. When you try to do an update if it is good it returns 1 and when it fails it returns undef. Thus the program calling it can check to see if it worked or not.

I was told here previously to keep my HTML and my modules as seperate as possible. So when an error occurs two object vars are set and hold the type of error and the string the user should see and gived details on the error. Like input too long, needs to be unique, not an e-mail address using Email::Valid, ect.

So I made a program that calls the object and gets the errors when they occur. So far so good and all works fine ( after much tuning ) but then I try to do a simple substitution into an HTML string that looks like this:
my $error_html = "<font face=\"Arial, Helvetica, sans-serif\" size=\"2\" color=\"red\" +>error<\/font>";
so I tried this:
my $error_string = $error_html; $error_string =~ s/error/$user->get_error_string/; $template->param( user_name_error_string => $error_string );
And I get this:
User=HASH(0x91e14)->get_error_string

I tried quotes I have no clue how to get the string out short of pushing it to another var and then doing the substitution. Is there a way to do this without using another var?

Replies are listed 'Best First'.
Re: Substituting method from object
by Zaxo (Archbishop) on Dec 03, 2002 at 01:25 UTC

    To make the method be called in the right argument os s///, use the '/e' modifier:

    $error_string =~ s/error/$user->get_error_string/e;

    If not using templates, I'd prefer to just call &CGI::font or the corresponding font method.

    After Compline,
    Zaxo

Re: Substituting method from object
by pg (Canon) on Dec 03, 2002 at 03:21 UTC
    If you don't use /e modifier, it will not evaluate $user->get_error_string as an expression, which means it will not make the get_error_string function call for you.

    However it does resolve $user to its value, which is HASH(ox91e14) in this case.

    Hope this remove the confusion may rise. It does not evaluate the right side as expression, but does resolve the value of each valuable before substitution happens.

    One more example:
    $a = "Is this a bug?"; $a =~ s/bug/$& x 2/; print $a; # Is this a bug x 2? $a = "Is this a bug?"; $a =~ s/bug/$& x 2/e; print $a; # Is this a bugbug?
    In both case, $& is resolved to 'bug', but only in the second case, x is taken as an operator.
Re: Substituting method from object
by elusion (Curate) on Dec 03, 2002 at 01:27 UTC
    In a substitution, the replace value isn't evaluated, it's treated as a string. If you want it to be evaluated, you need to use the /e modifier.

    $error_string =~ s/error/$user->get_error_string/e;

    This is also how you would replace something with the value returned by a function or any other code.

    elusion : http://matt.diephouse.com

Re: Substituting method from object
by mce (Curate) on Dec 03, 2002 at 12:07 UTC
    Hi,

    I wrote a node about HTML::Template and the return of subs being used as variables 160153.

    It might be usefull


    ---------------------------
    Dr. Mark Ceulemans
    Senior Consultant
    IT Masters, Belgium

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-28 14:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found