On Thu, 14 Nov 2002 14:33:04 +1100, Ken Williams wrote: > >On Thursday, November 14, 2002, at 01:04 PM, Matthew O. Persico >wrote: > >>On Thu, 14 Nov 2002 11:49:56 +1100, Ken Williams wrote: >>> >>>Incidentally, I'd just write these as >>> >>>local $_ = $o->{lines}[0]; >> >>As explained, you usually use $_ as assigned by Perl in for, map >>,grep. From these snippets, it seems like $_ is being assigned and >>used because: >> >>1) being too lazy (bad lazy) to use a 'my' 2) misguidedly think >>that >>not using 'my' saves a significant number of cycles 3) being too >>cute. > >Actually, in some of them it looks like it's so that $_ can be >used as the implicit target of a regular expression. I do that >sometimes, particularly when writing something like a string >tokenizer, because it reduces clutter quite a bit: > >local $_ = shift(); >if ( /^foo/gc ) { >.... >} elsif ( /^bar/gc ) { >.... >} elsif ( /^baz/gc ) { >.... >} elsif ( /^quux/gc ) { >.... >}
TIMTOOTDI (and this is safer)... SWITCH: for(shift) { /^foo/gc && do { ...; last SWITCH; }; /^bar/gc && do { ...; last SWITCH; }; /^baz/gc && do { ...; last SWITCH; }; /^quux/gc && do { ...; last SWITCH; }; default: ## If we are here, nothing matched. ## Nothing special about the default: label ## in Perl, just an old K&R jockey at heart... ## And nothing special about the SWITCH label, ## either. I could have called it YO_MAMA:, but that ## is less self-documenting. :-) } In theory, as long as your tests are mutually exclusive, you do not need the "last SWITCH;" statements. However, since you are doing shift(), without the "last"s, you'll loop forever. Besides, it's safer to be explicit. -- Matthew O. Persico