On Thu, Aug 28, 2003 at 04:17:21PM -0400 K Old wrote: > Having been a Perl programmer for several years now I have become > accustom to using the following as my normal "start" of any Perl script: > > #!/usr/bin/perl > use warnings; > use strict; > > Randal Schwartz uses this: > > #!/usr/bin/perl -w > use strict; > $|++; > > Is there any difference between the -w and "use warnings" declaration? > I know that both turn on warnings and the -w is commonly used at the > command line, but was just curious as to if one was "better" than the other.
They are not the same thing, although somewhat related. The warnings pragma gives you very fine-graned control. It turns on warnings for blocks and can likewise be switched off for them. Furthermore, you can surpress particular warning categories altogether as in use warnings; $a = $b = "foobar"; print undef; { no warnings 'uninitialized'; print undef; print $a + $b; } print undef; __END__ Use of uninitialized value in print at - line 3. Argument "foobar" isn't numeric in addition (+) at - line 7. Argument "foobar" isn't numeric in addition (+) at - line 7. Use of uninitialized value in print at - line 9. As you see, no warning for line 6, but a warning for the line 7 in the same scope. The various categories that exist are listed in perllexwarn.pod. -w on the other hand is truely global. Turn it on anywhere and suddenly warnings show up from code that you haven't even written (like warnings produced in modules you use). They can be turned off lexically scoped by issuing local $^W = 0; though. But you can't distinguish between a warning caused by comparing a string with a number and, for instance, one caused by using undefined values. > One other item, Randal uses $|++; to "turn off the buffering for STDOUT". > What exactly is buffering of standard output? Buffering means that the data you send to a channel isn't going through right away but ends up in a buffer. STDOUT is typically line-buffered. That means, data go to STDOUT but wont be printed before a newline was sent (or the buffer is full). This is an efficiency thing because it reduces the number of syscalls. STDERR is typically not buffered which explains why you often get messages to STDERR before the regular output to STDOUT. Tassilo -- $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({ pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#; $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]