[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

Re: Bug in magic locals vs block loops? 20000128.003



[Larry Wall graciously allows]
> Not that Perl doesn't have its share of incomprehensible features,
> but I think this one's a bit of buglette too.  Especially
> considering that it works "right" in Perl 4.

Thanks for confirming that my years of slavery to that other P language
haven't wholly ruined my God-given capacity for intuiting Perl <wink>.

> On the other hand, since the buglette is related to the
> optimization of not saving and restoring the pattern match context
> around every loop iteration, we might be slow to fix it, if we
> can't figure a decent way to do it.  Fact is that almost nobody
> ever writes loop conditions with !~,

That was my first thought too (I didn't write the original code, just helped
a coworker track it down) -- "weird!", thought I.  However, he came up with
a killer response:  "so why doesn't it work?".

> and the buglette won't show up with =~ because then the pattern
> doesn't match on the last iteration, and $1 wouldn't be set anyway.

As another followup noted, and as another part of the test case showed, the
same problem occurs with =~ provided that the "while" is changed to "until".
The program was chewing thru a log file where it was much easier to
recognize interesting lines than (the more-frequenct) junk lines, so

until (<> =~ /$interesting/o) {
    # chew the junk
}
# do something interesting with the interesting line

is really quite natural!  It was only relative lack of Perl experience that
caused the more convoluted

while (<> !~ /$interesing/o) ...

form.

> Plus nowadays there's a bit of a magical lexical scope around
> entire while loops and if statements as well.  (Not that your
> buglette is lexical, it's dynamic.)  But anyway, if you write:
>
>    $wink = "y'rs\n";
>    if (my $wink = "wink-ly ") {
>	print $wink;
>    }
>    print "$wink";
>
> You'll find that it prints "wink-ly y'rs", despite the fact that
> the my is not within a syntactic block.  I suspect that's a feature.

While tempted to accuse you of blatant misdirection here <wink>, I agree --
that's a feature.  I see it as shorthand for introducing a lexical block
vrbl in the test condition -- convenient and helpful!  No problem.

> Your buglette is a bit less of a feature, I'd say.
>
> Mind, I'm not promising we won't fix it.  I don't make promises
> like that.  :-)

It proved easy to work around (just do explicit assignment to a "real vrbl"
in the test), so it's surely not a big deal.  The other side is that he
spent an hour puzzling over the docs unable to find a reason for why his
code wasn't working.  In retrospect, I'm kinda amazed I never bumped into it
either!

> Nice to hear from you after your long sojourn in snake-strewn lands!

Nice, too, to be heard.  Thanks for your insights here, Larry!

i-only-hang-out-with-pythons-cuz-the-pay-is-so-good<wink>-ly y'rs  - tim



[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]