Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

auto-escaping??

by goonfest (Sexton)
on Dec 13, 2001 at 01:42 UTC ( [id://131424]=perlquestion: print w/replies, xml ) Need Help??

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

I don't know if I gave this a correct title or not, but this is what I want to do. Let's say I have code as follows:
my $var1 = 1; my $var2 = 2; my $result = $var1 + $var2; my $equation = &SOME_SORT_OF_SPECIAL_FUNCTION($result); print "$equation\n"; print "$result\n";
I want my output to be:
$var1 + $var2
3

or better yet:
var1 + var2
3

What I am looking for is an existing Perl function and/or module and/or method that will, what I term, 'auto-escape' the value of any variable I pass to it, OR take it's literal contents, and if possible, get rid of any dollar-signs or brackets or braces. This is a simple example. My application would require auto-escaping of some more complex data structures, such as array and hash values. Thanks in advance for your help. I'm trying to comb the archives, but again, I don't know what this is officially called.

"Be proud, be a Goon"

Replies are listed 'Best First'.
(Ovid) Re: auto-escaping??
by Ovid (Cardinal) on Dec 13, 2001 at 02:03 UTC

    I think you're going to be disappointed. You can't just take a result and get the function that generated that result or, for that matter, even get the variable name associated with a give variable. One way would be to store variable names in variables, combine them at will and eval the result. Personally, I think this will cause you grief, though.

    my $bad_idea1 = '$var1'; my $bad_idea2 = '$var2'; my $var1 = 3; my $var2 = 4; my $equation = "$bad_idea1 + $bad_idea2"; print "$equation\n"; print eval $equation;

    Here's an example of how things can go wrong:

    my $bad_idea1 = '$var1'; my $bad_idea2 = '$var2'; my $var1 = 3; my $var2 = 0; my $equation = "$bad_idea1 / $bad_idea2"; print "$equation\n"; my $result = eval $equation; if ( $@ ) { print $@; } else { print $result; }

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: auto-escaping??
by belg4mit (Prior) on Dec 13, 2001 at 02:02 UTC
    Consider eval for use with your 2nd print, and using non-interpolatnig quotes "'" with concatenation "." for your 3rd assignment so your first print is as you expect.

    Of course there are issues with this, depending on where you're getting the values for $var1 and $var2, they could contain potentially nasty commands that would be run in the eval. See perlsec.

    --
    perl -p -e "s/(?:\w);([st])/'\$1/mg"

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (8)
As of 2024-04-19 13:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found