List Info

Thread: Re: repeated use of grep




Re: repeated use of grep
country flaguser name
United States
2007-06-12 19:14:42

>>&gt;>> "Glenn" == Glenn Becker < burningc%40sdf.lonestar.org">burningcsdf.lonestar.org&gt; writes:

>> This is really dangerous.... don't do this. Don't use $1 unless
>>; you've also checked that the match has succeeded.

Glenn> Would this be a reasonable place to use "eval" -- say, wrap the two
Glenn> lines above in an "eval" block? like the following

Glenn&gt; eval { /:s+(.*tgz$)/;
Glenn> push packages, $1 };
Glenn> print "No match occurred!n:$" if $;

No, it won't throw an exception if it doesn't match. Just follow the
guidelines. Use it in a conditional if you *expect* it not to match
sometimes. Use it with "or die" if you *never* expect it not to match.

Glenn> Is the danger of using $1 that, if the match does not succeed, you are
Glenn> accessing a "left-over" $1 from another match?

Yes. Or just garbage.... after all, even if $1 was undef on a non-match, your
code would be wrong, because it would push undef into packages, still ruining
the algorithm.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<; merlyn%40stonehenge.com">merlynstonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

__._,_.___
.

__,_._,___
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )