CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
eval { $dynamic_pattern_match }; doesnt work with activeperl...

 



gregaiken
New User

Jun 5, 2008, 3:35 PM

Post #1 of 5 (4805 views)
eval { $dynamic_pattern_match }; doesnt work with activeperl... Can't Post

maybe im missing something. but i would like to do a pattern match where the thing to be matched is a VARIABLE, not a previously known value to be hard-coded into the regex.

i assign the regex program line to a scalar, then eval the scalar.

on the surface, this should work (and i can swear that in the very old days with perl 4.036, it did). now that i run activeperl 5.8.8 build 822 this ability seems not to work for me.

here is my simple code demonstrating what i am attempting to do:



$word = 'word2';
$paragraph = 'word1 word2 word3';
$block = "\x24paragraph =~ \x2F$word\x2Fi;";

print "block to eval = $block\n";

eval { $block }; warn $@ if $@;

if ($') {
print "what follows the match = $'\n";
} else {
print "why wasnt '$word' found in '$paragraph'????\n";
}

running it displays clearly that 'word2' is not found within the string containing 'word1 word2 word3'.

i dont understand why this simple example does not work.


KevinR
Veteran


Jun 6, 2008, 9:12 AM

Post #2 of 5 (4755 views)
Re: [gregaiken] eval { $dynamic_pattern_match }; doesnt work with activeperl... [In reply to] Can't Post

Why are you even using eval and \x?




Code
   

$word = 'word2';
$paragraph = 'word1 word2 word3';
if ($paragraph =~ /$word/i) {

print "Match";

}

else {

print "No match";

}

-------------------------------------------------


gregaiken
New User

Jun 6, 2008, 3:43 PM

Post #3 of 5 (4746 views)
Re: [KevinR] eval { $dynamic_pattern_match }; doesnt work with activeperl... [In reply to] Can't Post

to answer you... in all of the perl regex code ive ever written or seen contributed by others, ive, until now, not seen the form you present, where a scalar is seen as a part of the regex (/$var/).

with my limited knowledge, i had assumed i needed to somehow create the regex command in a form where the variable substitution was previously done. hence my attempt to build a regex command (using \x## for special characters) then evaling it.

your approach is so simple, that i never would have guessed it could have worked.

by the way, at a different perl mailing list, another perl guru found the reason my code did not work, as initially written, was becaues a few variables needed to be predeclared by the use of 'my'. by declaring the variables i used, my code magically worked.

thank you much for the simple elegance of your suggestion.


meloyelo
User

Jun 9, 2008, 5:26 AM

Post #4 of 5 (4618 views)
Re: [gregaiken] eval { $dynamic_pattern_match }; doesnt work with activeperl... [In reply to] Can't Post

You want:

Code
eval $block;

What you have, eval { $block }, simply returns the value held in the variable $block.


(This post was edited by meloyelo on Jun 9, 2008, 5:27 AM)


KevinR
Veteran


Jun 9, 2008, 11:43 AM

Post #5 of 5 (4610 views)
Re: [gregaiken] eval { $dynamic_pattern_match }; doesnt work with activeperl... [In reply to] Can't Post


In Reply To
by the way, at a different perl mailing list, another perl guru found the reason my code did not work, as initially written, was becaues a few variables needed to be predeclared by the use of 'my'. by declaring the variables i used, my code magically worked.


Thats odd because if you are using "strict" your code would not have even compiled to know if it worked or not as you had it written. But maybe on the mailing list you included the fact you were getting an error about variables not being packaged.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives