On Thu, 14 Nov 2002 11:49:56 +1100, Ken Williams wrote: > >On Thursday, November 14, 2002, at 07:17 AM, Brian Ingerson wrote: > >>On 13/11/02 16:08 +0100, [EMAIL PROTECTED] wrote: >>> >>>A problem we had with dynamic loading of Inline modules has been >>>tracked down to the use of $_ in Inline::denter. >>>With the attached patch it works OK >> >>Wow. That's crazy. How could $_ affect anything? Would you mind >>sending a brief expanation? I'll apply the patch. > >$_ is a global variable just like any other. If you clobber it, you >may clobber someone else's data. > >The reason $_ is usually safe is that you don't generally assign >things to it, you let perl do the assigning implicitly (by using a >foreach(), map(), grep(), etc.) and in this case it localizes the >variable before assignment. > >But if you assign explicitly and you don't localize, clobber city.
The code snippet $_ = is usually a BAD THING. Don't do it. >The following script demonstrates: > >#!/usr/bin/perl > >top(); > >sub top { local $_ = 5; >inner(); >print "\$_ is $_\n"; >} > >sub inner { $_ = 7; >} > >__END__ > > >>*************** *** 188,193 **** --- 189,195 ---- >>$o->{done}++, $o->{level} = -1, return unless @{$o->{lines}}; >>my ($width, $tabwidth) = @{$o}{qw(width tabwidth)}; >>while (1) { + local ($_); >>$_ = $o->{lines}[0]; > >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. Instead of local ($_); $_ = $o->{lines}[0]; $o->next_line, next if /^(\s*$|\#)/; # skip comments and blank lines use a my $x and write my $x = $o->{lines}[0]; $o->next_line, next if $x=~ m/^(\s*$|\#)/; # skip comments and blank lines Or, even more concisely, $o->next_line, next if $o->{lines}[0];=~m/^(\s*$|\#)/; # skip comments and blank lines And in the case of this fragment: sub indent_data { my $o = shift; local ($_); $_ = shift; return $o->indent_undef($_) why not: sub indent_data { my $o = shift; return $o->indent_undef(shift); or even: sub indent_data { return shift()->indent_undef(shift); Yes, that last one really works! I tested it. talk about DWIM. -- Matthew O. Persico