I confess that I don't use the '-w' flag much in Perl except when I do syntax checking like:
$ perl -wc <script>
For the most part, the only warning perl issued during runtime for my programs 'Use of uninitialized value
Today, I found a Perl "feature" that I nearly reported as a bug. Take a look at this code:
use strict;
bar("value");
print "end\n";
sub bar {
for ($_[0]) {
/[a-z]/ && do {
print "Plunging into foo()\n";
foo();
print "You got me!\n";
return 1;
};
}
print "fall through\n";
return 1;
}
sub foo {
my $ans = '';
print "type 'q' to quit\n";
do{
last if lc (substr($ans, 0, 1)) eq 'q';
print "\nstill in loop\n";
} while( $ans = <> );
return;
}
Earlier today, I would have expected this program to print:
Plunging into foo()
type 'q' to quit
You got me
end
This expectation would have also gone unfulfilled, since the real output is more like:
Plunging into foo()
type 'q' to quit
still in loop
fall through
end
What's going on here? It looks like the 'last' in foo() is jumping back to the loop in bar(). In fact, this is the case and -w provides some insight:
Exiting subroutine via last at
...
Kudos to p5p for adding this warning! I suspect the problem lies with the infrequently-seen-in-Perl-but-useful-Pascal construction 'do{}while()'. Since I haven't run into this behavior before and I rarely use 'do{}while(),' I'm pointing fingers at it. Still this behavior seems a little bold to me. What do you think?
Uhhm, yeah, but (Score:1)
I always use 'perl -w'. I almost always find that 'Use of unitialized value' inicates a mistake that I need to correct. Sure, if you are testing for defined(), then the code can catch this and do something intelligent, but that's not usually the case in my code.
Most typically, the use of an unitiali
Re:Uhhm, yeah, but (Score:1)
Maybe I don't understand what you're getting at or maybe our coding styles are radically different, but I pretty much always want to be warned that I'm using an unitialized value.
I do 'use strict,' so don't take me for a total slacker. I haven't seen many situations in my code that the 'uninitialize' warning is helpful. With 'use strict' and a -wc check, I can catch typos at compile time. After that, I normally *depend* on values being uninitialized. For instance:
Re:Uhhm, yeah, but (Score:1)
.=or+=(or|=, for that matter). I think it did in some older versions of Perl.Can you give an example of code where the "uninitialized" warning gets in your way?
Re:Uhhm, yeah, but (Score:1)
Re:Uhhm, yeah, but (Score:1)
Re:Uhhm, yeah, but (Score:1)
Yes, it is lame, but IMO, so is the warning itself.
Re:Uhhm, yeah, but (Score:1)
do is not a sub (Score:1)
one tries to treat do like sub. You can't return from a do,
or other such things (or at least I couldn't). I tend to set the
return value of a do just by having the value as the last expression.
As it says in perldoc, really.
---ict / Spoon
Yessir (Score:2)
The standard trick solution is to double-up the curlies, creating a block from which you can exit:
Uuuuugly. That's the price you pay for having trailing modifiers. In perl5 at least ...
--Nat
Re:Yessir (Score:1)
Thanks for the tip, yet I confess that this behavior violates the design principle of least astonishment. Perhaps I'm letting my Pascal heritage show a bit too much. The other wacky behavior is that the last jumps out of foo() back into bar(). I realize that next, last and redo are different spellings for goto. Still, I was surprised. In any case, I'm happy that someone had the foresight to recognize that this behavior might startle some users and to provide a warning to clue us in.
This is one of the dusty
Re:Yessir (Score:2)
Yes, it is surprising.
This is a legacy from Rift Valley Perl, I believe.
--Nat
Re:Yessir (Score:1)
Can someone please enlighten me what "Rift Valley Perl" is?
Re:Yessir (Score:2)
Yeah, I know, Dennis Miller's show was cancelled. Get a life, Torkington :-)
--Nat