Greg,

im a humble bloke - such salutations are unnecessary - thanks anyway, i help
when i can.

strict and warnings when added dont do anything much to your code per se,
however it is good programming practise, and a mantra for all who advocate
Perl advise on this list and most of the others. you could safely remove
them from your code and still get it to execute accordingly.

that said the effect of predeclaring the variable outside the scope of the
eval braces makes that variable visible outside of the eval. all variables
(built in or not) are localised to their respective braces, turning strict
on enforces this. assigning a variable visible outside of the scope to the
value inside a scope has the benefit of making the data inside the scope
available outside. im sure you knew all of that anyway, i have a notion that
you code with some other language mainly.

Some points:-

   - the nature you describe with perl4 on win95 was a feature albeit a
   flawed one ; )
   - strict and warnings should always be the first 2 lines of your code -
   perl6 enables them by default - you wont have much choice
   - pre and post match regexp operators are expensive - dont use them if
   you dont have to
   - Tobias' use of qr (compiling the regexp) is the preferred method

cheers

Just in


2008/6/7 Greg Aiken <[EMAIL PROTECTED]>:

>  sir, you are a god!  thanks so very much here for your insights.
>
>
>
> I would understand if you don't have much more time here for me.
>
> but if you might, it would help me to understand perl better if you could
> explain exactly why the code would not run as I originally coded it.
>
>
>
> why does including strict pragma and predeclaring the variables help in
> this case?
>
>
>  ------------------------------
>
> *From:* Justin Allegakoen [mailto:[EMAIL PROTECTED]
> *Sent:* Thursday, June 05, 2008 8:05 PM
> *To:* [EMAIL PROTECTED]
> *Cc:* [email protected]
> *Subject:* Re: eval { $variable_regex_command }; seems not to work for
> me...
>
>
>
>
>
> --------------8<------------------------
>
> i would like to do a pattern match where the thing to be matched is a
> $VARIABLE, not a previously known value that could otherwise be simply
> 'hard-coded' into the regex (as most regex users do).  I need to do
> something more dynamic than that…
>
> so 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 on win95, it actually did).  but now that i run
> activeperl 5.8.8 build 822 on xp this ability seems not to work for me.  of
> course, this could also be my mis understanding, or my mistake.  perhaps
> those with more expertise than myself can take a quick look at my code.
>
> here is 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.
>
> your help would be GREATLY appreciated.
>
> --------------8<------------------------
>
>
>
> use strict;
> use warnings;
>
>
>
> my $post_match = 0;
> my $word      = 'word2';
> my $paragraph = 'word1 word2 word3';
> my $block = "\x24paragraph =~ \x2F$word\x2Fi;";
>
>
>
> print "block to eval = $block\n";
>
>
>
> eval { $paragraph =~ /$word/i; $post_match = $' };
> warn $@ if $@;
>
>
>
> if ($post_match)
> {
>     print "what follows the match = $post_match\n";
> }
> else
> {
>     print "why wasnt '$word' found in '$paragraph'????\n";
> }
>
>
>
> __END__
>
>
>
> HTH,
>
> Just in
>
>
>



-- 
Justin Allegakoen
Vulcan Technical Services
Level 2, 190 Aberdeen St.
Northbridge, WA 6003
(08) 6211 0000
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to