Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > variables inside a string

Reply
Thread Tools

variables inside a string

 
 
Martin Keiter
Guest
Posts: n/a
 
      12-03-2010
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?


 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      12-03-2010
Martin Keiter <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Martin Keiter
Guest
Posts: n/a
 
      12-03-2010

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!
 
Reply With Quote
 
Wolf Behrenhoff
Guest
Posts: n/a
 
      12-03-2010
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
 
Reply With Quote
 
Krishna Chaitanya
Guest
Posts: n/a
 
      12-03-2010
On Dec 3, 3:55*pm, Martin Keiter <(E-Mail Removed)> 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.
 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      12-03-2010
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
 
Reply With Quote
 
Martin Keiter
Guest
Posts: n/a
 
      12-03-2010
On 2010-12-03, Krishna Chaitanya <(E-Mail Removed)> 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!
 
Reply With Quote
 
Martin Keiter
Guest
Posts: n/a
 
      12-03-2010
On 2010-12-03, Wolf Behrenhoff <(E-Mail Removed)> 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!
 
Reply With Quote
 
Martin Keiter
Guest
Posts: n/a
 
      12-03-2010
On 2010-12-03, Martijn Lievaart <(E-Mail Removed)> 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.
 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      12-03-2010
Martin Keiter wrote:
> On 2010-12-03, Martijn Lievaart <(E-Mail Removed)> 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"; }
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding a string inside string arnuld C Programming 3 11-23-2010 09:49 AM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
good practice to initalize all instance variables with type String to emptry string?? Matt Java 4 06-23-2004 06:14 AM
Problem with inside to inside traffic after upgrading PIX 515 Cisco 5 06-15-2004 06:34 AM
Dynamic temp. datagrid col.gen. -Session access inside a class inside a UserCtrl Andy Eshtry ASP .Net 0 03-01-2004 11:48 PM



Advertisments