Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   variables inside a string (http://www.velocityreviews.com/forums/t914273-variables-inside-a-string.html)

Martin Keiter 12-03-2010 09:32 AM

variables inside a string
 
Hi,

I would like to do something like this:

$sentence = "$1 is here!";
....
if ($string =~ m/(.*) comes in/) {
print $sentence;
}

This of course does not work, as $1 is evaluated when $sentence is
initialized. But also setting

$sentence = "\$1 is here!"

does not work - it just prints

$1 is here!

is there a way to evaluate th "$1" inside of $sentence at the time when
$sentence is used?



Jürgen Exner 12-03-2010 10:39 AM

Re: variables inside a string
 
Martin Keiter <Martin.Keiter_NOSPAM@gmx.de> wrote:
>is there a way to evaluate th "$1" inside of $sentence at the time when
>$sentence is used?


If you want to evaluate a string I suggest you look at the eval()
function.

jue

Martin Keiter 12-03-2010 10:55 AM

Re: variables inside a string
 

Ok, I found "eval", and it works, although I do not yet fully understand
why :-)

This does what I want:

my $sentence = "\$1 is here!";
my $string = "foo comes in";

if ($string =~ m/(.*) comes in/) {
my $code =" \"$sentence\" ";
print "code: $code\n";
my $bar = eval $code;
print "$bar\n";
}

but do I really have to introduce this additional variable $code ?
I tried:
eval \"$sentence\";
eval "$sentence";
eval $sentence;
but all of them produce errors...



--
Meine Mailadresse funktioniert!

Wolf Behrenhoff 12-03-2010 11:09 AM

Re: variables inside a string
 
On 03.12.2010 11:55, Martin Keiter wrote:
> Ok, I found "eval", and it works, although I do not yet fully understand
> why :-)
>
> This does what I want:
>
> my $sentence = "\$1 is here!";

^^^^
> my $string = "foo comes in";
>
> if ($string =~ m/(.*) comes in/) {
> my $code =" \"$sentence\" ";

^^^^^^^^^^^^^^^^^^
> print "code: $code\n";
> my $bar = eval $code;
> print "$bar\n";
> }


That's awful to read. Don't use too many \ if they can be avoided
easily. Use q (or ') or qq instead.

> but do I really have to introduce this additional variable $code ?
> I tried:


Try
print eval qq("$sentence");

Another way without eval is:

sub sentence { "$1 is here" }
.... print sentence;


Wolf

Krishna Chaitanya 12-03-2010 11:20 AM

Re: variables inside a string
 
On Dec 3, 3:55*pm, Martin Keiter <Martin.Keiter_NOS...@gmx.de> wrote:
[text removed]

It may help you to check "How can I expand variables in text strings?"
in Perl FAQ 4. Your example's harmless for educational purposes, but,
for any serious code, be careful for any unpredictability or tainted
text in $string (especially if it comes from an outside source). Also,
if you use eval, check for $@. Of course, nothing beats avoiding this
problem in the 1st place...don't have double interpolations unless
you've a very good reason to.

Martijn Lievaart 12-03-2010 12:22 PM

Re: variables inside a string
 
On Fri, 03 Dec 2010 09:32:42 +0000, Martin Keiter wrote:

> Hi,
>
> I would like to do something like this:
>
> $sentence = "$1 is here!";
> ...
> if ($string =~ m/(.*) comes in/) {
> print $sentence;
> }
>
> This of course does not work, as $1 is evaluated when $sentence is
> initialized. But also setting
>
> $sentence = "\$1 is here!"
>
> does not work - it just prints
>
> $1 is here!
>
> is there a way to evaluate th "$1" inside of $sentence at the time when
> $sentence is used?


$sentence =~ s/\$1/$1/;

HTH,
M4

Martin Keiter 12-03-2010 02:32 PM

Re: variables inside a string
 
On 2010-12-03, Krishna Chaitanya <schaitan@gmail.com> wrote:
>
> It may help you to check "How can I expand variables in text strings?"
> in Perl FAQ 4. Your example's harmless for educational purposes, but,
> for any serious code, be careful for any unpredictability or tainted
> text in $string (especially if it comes from an outside source). Also,
> if you use eval, check for $@. Of course, nothing beats avoiding this
> problem in the 1st place...don't have double interpolations unless
> you've a very good reason to.


As I only today discovered "eval" I was not aware of potential dangers.
Thank you very much for pointing that out!
(yes, the string will come from an outside source!)

--
Meine Mailadresse funktioniert!

Martin Keiter 12-03-2010 02:34 PM

Re: variables inside a string
 
On 2010-12-03, Wolf Behrenhoff <NoSpamPleaseButThisIsValid3@gmx.net> wrote:
> On 03.12.2010 11:55, Martin Keiter wrote:
>> my $code =" \"$sentence\" ";

> ^^^^^^^^^^^^^^^^^^
>
> That's awful to read. Don't use too many \ if they can be avoided
> easily. Use q (or ') or qq instead.


yes, it is!

>> but do I really have to introduce this additional variable $code ?
>> I tried:

>
> Try
> print eval qq("$sentence");


looks much better - thanks!

--
Meine Mailadresse funktioniert!

Martin Keiter 12-03-2010 02:38 PM

Re: variables inside a string
 
On 2010-12-03, Martijn Lievaart <m@rtij.nl.invlalid> wrote:

> $sentence =~ s/\$1/$1/;


this produces:
Use of uninitialized value in substitution iterator at .//test.pl line 13.

what I do now is:

my $sentence = "\$1 is here!";
my $string = "foo comes in";
my $reg = "(.*) comes in";

my $text = $sentence;
if ($string =~ m/$reg/)
{
foreach my $i (1..9) {
if (${$i}) {
my $tmp = ${$i};
$text =~ s/\$$i/$tmp/
}
}
print "$text\n";
}


no eval any more, so it should be safe, although I find it a bit ugly.

J. Gleixner 12-03-2010 03:06 PM

Re: variables inside a string
 
Martin Keiter wrote:
> On 2010-12-03, Martijn Lievaart <m@rtij.nl.invlalid> wrote:
>
>> $sentence =~ s/\$1/$1/;

>
> this produces:
> Use of uninitialized value in substitution iterator at .//test.pl line 13.
>
> what I do now is:
>
> my $sentence = "\$1 is here!";
> my $string = "foo comes in";
> my $reg = "(.*) comes in";
>
> my $text = $sentence;
> if ($string =~ m/$reg/)
> {
> foreach my $i (1..9) {
> if (${$i}) {
> my $tmp = ${$i};
> $text =~ s/\$$i/$tmp/
> }
> }
> print "$text\n";
> }
>
>
> no eval any more, so it should be safe, although I find it a bit ugly.


Maybe you're making this more complex than you need to. Provided your
example is close to your actual code you could simplify it a lot.

if( $string =~ m/$reg/ ){ print "$1 is here"; }


All times are GMT. The time now is 05:53 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.