[31963] in Perl-Users-Digest
Perl-Users Digest, Issue: 3227 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Dec 3 14:09:36 2010
Date: Fri, 3 Dec 2010 11:09:21 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 3 Dec 2010 Volume: 11 Number: 3227
Today's topics:
Hash key iteration order <schaitan@gmail.com>
Re: Hash key iteration order <tadmc@seesig.invalid>
Re: Hash key iteration order <schaitan@gmail.com>
Re: Hash key iteration order sln@netherlands.com
Re: Optimisation for tight loops... <marc.girod@gmail.com>
variables inside a string <Martin.Keiter_NOSPAM@gmx.de>
Re: variables inside a string <jurgenex@hotmail.com>
Re: variables inside a string <Martin.Keiter_NOSPAM@gmx.de>
Re: variables inside a string <NoSpamPleaseButThisIsValid3@gmx.net>
Re: variables inside a string <schaitan@gmail.com>
Re: variables inside a string <m@rtij.nl.invlalid>
Re: variables inside a string <Martin.Keiter_NOSPAM@gmx.de>
Re: variables inside a string <Martin.Keiter_NOSPAM@gmx.de>
Re: variables inside a string <Martin.Keiter_NOSPAM@gmx.de>
Re: variables inside a string <glex_no-spam@qwest-spam-no.invalid>
Re: variables inside a string <jurgenex@hotmail.com>
Re: variables inside a string <Martin.Keiter_NOSPAM@gmx.de>
Re: variables inside a string <Martin.Keiter_NOSPAM@gmx.de>
Re: variables inside a string sln@netherlands.com
Re: variables inside a string <willem@turtle.stack.nl>
Re: variables inside a string <willem@turtle.stack.nl>
Re: variables inside a string <Martin.Keiter_NOSPAM@gmx.de>
Re: variables inside a string <Martin.Keiter_NOSPAM@gmx.de>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 3 Dec 2010 00:49:05 -0800 (PST)
From: Krishna Chaitanya <schaitan@gmail.com>
Subject: Hash key iteration order
Message-Id: <cd986dd8-1975-4c0f-91f6-df6408a4722d@o23g2000prh.googlegroups.com>
Hi, I was learning and reading about hash iterators. I am using Perl
5.8.8, and found that between 10 runs of Perl, the hash keys show up
in the same order.
======================
[kc@imits094 Perl]# for i in 1 2 3 4 5 6 7 8 9 10; do ./test.pl ; done
Keys of a are 6 3 7 9 2 8 1 4 10 5
Keys of a are 6 3 7 9 2 8 1 4 10 5
Keys of a are 6 3 7 9 2 8 1 4 10 5
Keys of a are 6 3 7 9 2 8 1 4 10 5
Keys of a are 6 3 7 9 2 8 1 4 10 5
Keys of a are 6 3 7 9 2 8 1 4 10 5
Keys of a are 6 3 7 9 2 8 1 4 10 5
Keys of a are 6 3 7 9 2 8 1 4 10 5
Keys of a are 6 3 7 9 2 8 1 4 10 5
Keys of a are 6 3 7 9 2 8 1 4 10 5
[kc@imits094 Perl]# cat test.pl
#!/usr/bin/perl
my %a;
@a{1..10} = ();
print "Keys of a are ", join(' ',keys %a), "\n";
==================
The description of "keys" function says: "Since Perl 5.8.1 the
ordering is different even between different runs of Perl for security
reasons (see Algorithmic Complexity Attacks in perlsec)."
And the perlsec page says: "In Perl 5.8.1 the random perturbation was
done by default, but as of 5.8.2 it is only used on individual hashes
if the internals detect the insertion of pathological data. If one
wants for some reason emulate the old behaviour (and expose oneself to
DoS attacks) one can set the environment variable PERL_HASH_SEED to
zero to disable the protection (or any other integer to force a known
perturbation, rather than random). One possible reason for wanting to
emulate the old behaviour is that in the new behaviour consecutive
runs of Perl will order hash keys differently, which may confuse some
applications (like Data::Dumper: the outputs of two different runs are
no longer identical)."
I'm confused...the ordering "is" different? or "may be" different
based on "insertion of pathological data"? But again, "the new
behaviour" above talks about different ordering of hash keys. In my
example, I don't think 1..10 is pathological data for keys...
Add to all this: "Also, the ordering of hash keys has always been, and
continues to be, affected by the insertion order."
I am totally out of my element here...can anyone shed some light on
this, please?
-KC
------------------------------
Date: Fri, 03 Dec 2010 08:13:16 -0600
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: Hash key iteration order
Message-Id: <slrnifhvd2.81s.tadmc@tadbox.sbcglobal.net>
Krishna Chaitanya <schaitan@gmail.com> wrote:
> Hi, I was learning and reading about hash iterators. I am using Perl
> 5.8.8, and found that between 10 runs of Perl, the hash keys show up
> in the same order.
> I am totally out of my element here...can anyone shed some light on
> this, please?
If you can obey this maxim:
Never rely on the order of hash keys.
Then you can ignore this question and move on to learning something
that you might be able to actually make use of. :-)
See also:
http://www.perlmonks.org/?node_id=629429
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
------------------------------
Date: Fri, 3 Dec 2010 07:01:40 -0800 (PST)
From: Krishna Chaitanya <schaitan@gmail.com>
Subject: Re: Hash key iteration order
Message-Id: <0d872465-7e66-488d-a0fc-616dbfbd5329@22g2000prx.googlegroups.com>
On Dec 3, 7:13=A0pm, Tad McClellan <ta...@seesig.invalid> wrote:
[snipped]
>
> If you can obey this maxim:
>
> =A0 =A0 Never rely on the order of hash keys.
>
> Then you can ignore this question and move on to learning something
> that you might be able to actually make use of. =A0:-)
>
> See also:
>
> =A0 =A0http://www.perlmonks.org/?node_id=3D629429
>
Thanks a lot. That perlmonks link was curiously my very question, and
the answers there cleared some air. I actually do know the maxim which
you stated above and have followed it so far, but I got terminally
curious while reading up on functions like each and finding differing
tones regarding this matter in the documentation. I just wanted to
experiment with the iterator being common to each, keys, and values in
a single run, but was puzzled to see it behaving "predictably" across
runs too. That's what made me ask you folks here.
And yeah, I wouldn't consider this as useless information :-) Who
knows, I could spot this problem in the code I inherited here at work,
or even help others who aren't aware of it. Have a nice day.
------------------------------
Date: Fri, 03 Dec 2010 09:10:38 -0800
From: sln@netherlands.com
Subject: Re: Hash key iteration order
Message-Id: <rf8if6lrbmoir23mvvbea5qmlfo8ce9ba7@4ax.com>
On Fri, 3 Dec 2010 00:49:05 -0800 (PST), Krishna Chaitanya <schaitan@gmail.com> wrote:
>Hi, I was learning and reading about hash iterators. I am using Perl
>5.8.8, and found that between 10 runs of Perl, the hash keys show up
>in the same order.
>
>======================
>
>[kc@imits094 Perl]# for i in 1 2 3 4 5 6 7 8 9 10; do ./test.pl ; done
>Keys of a are 6 3 7 9 2 8 1 4 10 5
>Keys of a are 6 3 7 9 2 8 1 4 10 5
>Keys of a are 6 3 7 9 2 8 1 4 10 5
>Keys of a are 6 3 7 9 2 8 1 4 10 5
>Keys of a are 6 3 7 9 2 8 1 4 10 5
>Keys of a are 6 3 7 9 2 8 1 4 10 5
>Keys of a are 6 3 7 9 2 8 1 4 10 5
>Keys of a are 6 3 7 9 2 8 1 4 10 5
>Keys of a are 6 3 7 9 2 8 1 4 10 5
>Keys of a are 6 3 7 9 2 8 1 4 10 5
>[kc@imits094 Perl]# cat test.pl
>#!/usr/bin/perl
>
>my %a;
>@a{1..10} = ();
>
>print "Keys of a are ", join(' ',keys %a), "\n";
>
>==================
>
[...]
>I'm confused...the ordering "is" different? or "may be" different
>based on "insertion of pathological data"? But again, "the new
>behaviour" above talks about different ordering of hash keys. In my
>example, I don't think 1..10 is pathological data for keys...
>
>Add to all this: "Also, the ordering of hash keys has always been, and
>continues to be, affected by the insertion order."
>
>I am totally out of my element here...can anyone shed some light on
>this, please?
>
I get different output within and between runs.
I added a funky add/delete key operation in the rand key
creation which throws off the order. Otherwise, even the
rand keys produce the same results as the slice
(the lines can be commented out for proof).
I would not have any ordering expectation for hash keys.
-sln
-------------------------
use strict;
use warnings;
for (1..5)
{
my (%a, @keys);
my $count = 1;
while ($count <= 10) {
$a{'938jun__+'} = ();
my $key = int( rand(11) );
if ($key > 0 && !exists( $a{$key} )) {
$a{$key} = ();
$count++;
push @keys, $key;
delete $a{'938jun__+'};
}
}
print "\nRand keys ... are @keys\n";
print "Keys of test1 are ", join(' ',keys %a), "\n";
%a = ();
@a{1..10} = ();
print "Keys of test2 are ", join(' ',keys %a), "\n";
}
__END__
Rand keys ... are 9 5 2 3 1 7 6 8 4 10
Keys of test1 are 6 3 7 9 2 8 4 1 10 5
Keys of test2 are 6 3 7 9 2 8 4 1 10 5
Rand keys ... are 6 3 9 2 4 5 10 8 1 7
Keys of test1 are 6 3 7 9 2 8 1 4 10 5
Keys of test2 are 6 3 7 9 2 8 4 1 10 5
Rand keys ... are 4 6 3 2 8 7 5 9 1 10
Keys of test1 are 6 3 7 9 2 8 1 4 10 5
Keys of test2 are 6 3 7 9 2 8 4 1 10 5
Rand keys ... are 4 3 5 7 9 10 8 2 6 1
Keys of test1 are 6 3 7 9 2 8 1 4 10 5
Keys of test2 are 6 3 7 9 2 8 4 1 10 5
Rand keys ... are 9 7 2 8 1 3 5 4 10 6
Keys of test1 are 6 3 7 9 2 8 4 1 10 5
Keys of test2 are 6 3 7 9 2 8 4 1 10 5
------------------------------
Date: Fri, 3 Dec 2010 03:04:25 -0800 (PST)
From: Marc Girod <marc.girod@gmail.com>
Subject: Re: Optimisation for tight loops...
Message-Id: <2a775510-39a5-456e-84be-3df9c852010d@o11g2000prf.googlegroups.com>
On Dec 3, 2:09=A0am, "Uri Guttman" <u...@StemSystems.com> wrote:
> and if that module loads another module? ad infinitum. welcome to the
> halting problem. do you even know what that is?
Looking from aside, I believe Keith does, and I believe your
comparison to the halting problem is incorrect.
The halting problem is a case of self-reference: to prove that any
program will halt, you must prove first that your own program will
halt.
There is no such problem with the optimization question (or there is
one in practice, but it has been repeatedly excluded).
Unless *this* is what you meant, but it wasn't clear.
Let *me* try to be clear: the optimization ought to be itself
optimized, so that it would benefit any other program.
Marc
------------------------------
Date: 3 Dec 2010 09:32:42 GMT
From: Martin Keiter <Martin.Keiter_NOSPAM@gmx.de>
Subject: variables inside a string
Message-Id: <slrnifhe9q.kl.Martin.Keiter_NOSPAM@egg38.theochem.uni-duesseldorf.de>
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?
------------------------------
Date: Fri, 03 Dec 2010 02:39:23 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: variables inside a string
Message-Id: <nuhhf6pbu3dcj2esdn3umd9ailfbagq05q@4ax.com>
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
------------------------------
Date: 3 Dec 2010 10:55:28 GMT
From: Martin Keiter <Martin.Keiter_NOSPAM@gmx.de>
Subject: Re: variables inside a string
Message-Id: <slrnifhj4v.kl.Martin.Keiter_NOSPAM@egg38.theochem.uni-duesseldorf.de>
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!
------------------------------
Date: Fri, 03 Dec 2010 12:09:28 +0100
From: Wolf Behrenhoff <NoSpamPleaseButThisIsValid3@gmx.net>
Subject: Re: variables inside a string
Message-Id: <4cf8cfe8$0$6765$9b4e6d93@newsspool3.arcor-online.net>
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
------------------------------
Date: Fri, 3 Dec 2010 03:20:35 -0800 (PST)
From: Krishna Chaitanya <schaitan@gmail.com>
Subject: Re: variables inside a string
Message-Id: <a3696d66-700f-4dbc-9888-e30efc642c32@r31g2000prg.googlegroups.com>
On Dec 3, 3:55=A0pm, 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.
------------------------------
Date: Fri, 3 Dec 2010 13:22:21 +0100
From: Martijn Lievaart <m@rtij.nl.invlalid>
Subject: Re: variables inside a string
Message-Id: <t04ns7-ss.ln1@news.rtij.nl>
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
------------------------------
Date: 3 Dec 2010 14:32:28 GMT
From: Martin Keiter <Martin.Keiter_NOSPAM@gmx.de>
Subject: Re: variables inside a string
Message-Id: <slrnifhvrs.kl.Martin.Keiter_NOSPAM@egg38.theochem.uni-duesseldorf.de>
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!
------------------------------
Date: 3 Dec 2010 14:34:05 GMT
From: Martin Keiter <Martin.Keiter_NOSPAM@gmx.de>
Subject: Re: variables inside a string
Message-Id: <slrnifhvus.kl.Martin.Keiter_NOSPAM@egg38.theochem.uni-duesseldorf.de>
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!
------------------------------
Date: 3 Dec 2010 14:38:29 GMT
From: Martin Keiter <Martin.Keiter_NOSPAM@gmx.de>
Subject: Re: variables inside a string
Message-Id: <slrnifi075.kl.Martin.Keiter_NOSPAM@egg38.theochem.uni-duesseldorf.de>
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.
------------------------------
Date: Fri, 03 Dec 2010 09:06:39 -0600
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: variables inside a string
Message-Id: <4cf8f6da$0$46848$815e3792@news.qwest.net>
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"; }
------------------------------
Date: Fri, 03 Dec 2010 08:05:37 -0800
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: variables inside a string
Message-Id: <ms4if6t6ktdi9tg4bcku4j888mve00rpc3@4ax.com>
Martin Keiter <Martin.Keiter_NOSPAM@gmx.de> wrote:
>Ok, I found "eval", and it works, although I do not yet fully understand
>why :-)
Actually it is very simple.
You just have to understand the difference between code and data.
>This does what I want:
>
>my $sentence = "\$1 is here!";
This line, including the double-quoted text is code. It is executed at
runtime and at that moment the content of the variable becomes data.
Obviously you cannot execute data.
> my $bar = eval $code;
Exception being eval(), which converts data (i.e. text) back into code,
such that it can be executed again.
> my $code =" \"$sentence\" ";
>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...
The thingy that you pass to eval must be legal Perl code.
$1 is here!
is not legal Perl code.
jue
------------------------------
Date: 3 Dec 2010 16:09:34 GMT
From: Martin Keiter <Martin.Keiter_NOSPAM@gmx.de>
Subject: Re: variables inside a string
Message-Id: <slrnifi5ht.kl.Martin.Keiter_NOSPAM@egg38.theochem.uni-duesseldorf.de>
On 2010-12-03, J. Gleixner <glex_no-spam@qwest-spam-no.invalid> wrote:
> 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"; }
the example is relatively close to my code, but of course I have
simplified a bit: actually I have regexps and corresponding sentences in
an array, and then do a test of $string against all regexps inside a
loop. Most of the sentences will not contain a "$1", but some will.
$test[0]{'regexp'} = "(.*) comes in";
$test[0]{'sentence'} = "\$1 is here!";
...
...
# actually this will come from the outside world:
my $string = "foo comes in";
foreach my $i (0..$#talk)
{
if ($string =~ m/$test[$1]{'regexp'} /)
{
$text = $test[$i]{'sentence'};
# now if there is a (XXX) inside of $test[$1]{'regexp'}
# replace $1 in $text by XXX
}
}
------------------------------
Date: 3 Dec 2010 16:16:56 GMT
From: Martin Keiter <Martin.Keiter_NOSPAM@gmx.de>
Subject: Re: variables inside a string
Message-Id: <slrnifi5vo.kl.Martin.Keiter_NOSPAM@egg38.theochem.uni-duesseldorf.de>
On 2010-12-03, Jürgen Exner <jurgenex@hotmail.com> wrote:
> Martin Keiter <Martin.Keiter_NOSPAM@gmx.de> wrote:
>>Ok, I found "eval", and it works, although I do not yet fully understand
>>why :-)
>
> Actually it is very simple.
So I hoped!
> You just have to understand the difference between code and data.
I think I did, but am quite shure, I do not yet understand all this
quoting stuff.
>>This does what I want:
>>
>>my $sentence = "\$1 is here!";
>
> This line, including the double-quoted text is code. It is executed at
> runtime and at that moment the content of the variable becomes data.
> Obviously you cannot execute data.
agreed :-)
>> my $bar = eval $code;
>
> Exception being eval(), which converts data (i.e. text) back into code,
> such that it can be executed again.
ok.
>> my $code =" \"$sentence\" ";
>>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...
>
> The thingy that you pass to eval must be legal Perl code.
> $1 is here!
> is not legal Perl code.
ok.
But why does this work then?
my $sentence = "\$1 is here!";
my $code =" \"$sentence\" ";
eval $code;
is it that
"$1 is here!"
is passed to eval (including the quotes) and this is a valid expression?
Thanks for your patience!
------------------------------
Date: Fri, 03 Dec 2010 09:55:11 -0800
From: sln@netherlands.com
Subject: Re: variables inside a string
Message-Id: <vjbif61583a048t7pn1hom6thf30eaocee@4ax.com>
On 3 Dec 2010 16:09:34 GMT, Martin Keiter <Martin.Keiter_NOSPAM@gmx.de> wrote:
>On 2010-12-03, J. Gleixner <glex_no-spam@qwest-spam-no.invalid> wrote:
>> 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"; }
>
>the example is relatively close to my code, but of course I have
>simplified a bit: actually I have regexps and corresponding sentences in
>an array, and then do a test of $string against all regexps inside a
>loop. Most of the sentences will not contain a "$1", but some will.
>
>
>$test[0]{'regexp'} = "(.*) comes in";
>$test[0]{'sentence'} = "\$1 is here!";
>...
>...
>
># actually this will come from the outside world:
>my $string = "foo comes in";
>
>foreach my $i (0..$#talk)
>{
> if ($string =~ m/$test[$1]{'regexp'} /)
> {
> $text = $test[$i]{'sentence'};
> # now if there is a (XXX) inside of $test[$1]{'regexp'}
> # replace $1 in $text by XXX
> }
>}
If you going to use eval, you could always use the suggs posted
by others with something like this.
-sln
use strict;
use warnings;
my @template = (
{ regexp => q{(.*) comes in},
sentence => qq{"\$1 is here!\\n"}
},
{ regexp => q{(.*) sometimes comes in},
sentence => qq{"\$1 is sometimes there!\\n"}
},
);
my @instrings = (
'foo comes in here',
'bar sometimes comes in',
'baz comes in here',
);
for my $sample (@instrings)
{
for my $i (0..$#template)
{
if ( $sample =~ /$template[ $i ]{'regexp'}/ ) {
print eval $template[ $i ]{'sentence'};
}
}
}
__END__
------------------------------
Date: Fri, 3 Dec 2010 17:59:50 +0000 (UTC)
From: Willem <willem@turtle.stack.nl>
Subject: Re: variables inside a string
Message-Id: <slrnific0m.1kbo.willem@turtle.stack.nl>
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.
Obviously, since the substitution itself makes a new $1.
) 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.
Try this:
if (my @matches = $string =~ m/$reg/) {
$text =~ s/\$(\d+)/$matches[$1]/g;
}
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
------------------------------
Date: Fri, 3 Dec 2010 18:02:09 +0000 (UTC)
From: Willem <willem@turtle.stack.nl>
Subject: Re: variables inside a string
Message-Id: <slrnific51.1kfc.willem@turtle.stack.nl>
Willem wrote:
) Try this:
)
) if (my @matches = $string =~ m/$reg/) {
) $text =~ s/\$(\d+)/$matches[$1]/g;
) }
Oops, off-by-one error.
if (my @matches = $string =~ m/$reg/) {
$text =~ s/\$(\d+)/$matches[$1-1]/g;
}
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
------------------------------
Date: 3 Dec 2010 18:24:12 GMT
From: Martin Keiter <Martin.Keiter_NOSPAM@gmx.de>
Subject: Re: variables inside a string
Message-Id: <slrnifidec.j3a.Martin.Keiter_NOSPAM@egg38.theochem.uni-duesseldorf.de>
On 2010-12-03, Willem <willem@turtle.stack.nl> wrote:
> Willem wrote:
> ) Try this:
> )
> ) if (my @matches = $string =~ m/$reg/) {
> ) $text =~ s/\$(\d+)/$matches[$1]/g;
> ) }
>
> Oops, off-by-one error.
>
> if (my @matches = $string =~ m/$reg/) {
> $text =~ s/\$(\d+)/$matches[$1-1]/g;
> }
>
That's good:
- it works
- I understand how it works
- it does not use eval :-)
thank you!
------------------------------
Date: 3 Dec 2010 18:31:34 GMT
From: Martin Keiter <Martin.Keiter_NOSPAM@gmx.de>
Subject: Re: variables inside a string
Message-Id: <slrnifids6.j3a.Martin.Keiter_NOSPAM@egg38.theochem.uni-duesseldorf.de>
On 2010-12-03, sln@netherlands.com <sln@netherlands.com> wrote:
> If you going to use eval, you could always use the suggs posted
> by others with something like this.
>
> -sln
>
> use strict;
> use warnings;
>
> my @template = (
> { regexp => q{(.*) comes in},
> sentence => qq{"\$1 is here!\\n"}
> },
> { regexp => q{(.*) sometimes comes in},
> sentence => qq{"\$1 is sometimes there!\\n"}
> },
> );
I definitely have to learn about all this quoting! (you don't need to
explain it - I'll read and learn!
> my @instrings = (
> 'foo comes in here',
> 'bar sometimes comes in',
> 'baz comes in here',
> );
>
> for my $sample (@instrings)
> {
> for my $i (0..$#template)
> {
> if ( $sample =~ /$template[ $i ]{'regexp'}/ ) {
> print eval $template[ $i ]{'sentence'};
> }
> }
> }
this also works. But is it not dangerous if the instrings are provided
by potentially dangerous users?
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 3227
***************************************