[JOB] Perl Software Developer and Database programmer

Andy Armstrong andy at hexten.net
Wed Feb 22 17:39:50 GMT 2006


On 22 Feb 2006, at 17:08, Lusercop wrote:
> In which case, why are you needing to localise it? Any situation where
> you need to localise it means that the scope of it is too large.

Recently, something like this:

#!/usr/bin/perl

sub bof (&@);

$_ = 'Hello, World';

bof {
     print "*** $_ ***\n";
} qw(One Two Three);

print "$_\n";

sub bof (&@) {
     my $cb = shift;
     for my $x (@_) {
         local $_ = $x;
         $cb->();
     }
}

That's a contrived example - if it was that simple the loop in bof  
could be rewritten as

     for (@_) {
         $cb->();
     }

and $_ would automatically be localised. In less trivial cases though  
it's nice to be able to write subs that look map or grep - which  
means not trashing $_.

> I'd humbly suggest that this "makes the code more fragile", but  
> perhaps
> I'm just talking as someone whose main commercial programming  
> experience
> up to a year ago was maintenance programming.

This from the man who suggested faking |local| using |my| and ended  
up with less robust, more obscure code :)

-- 
Andy Armstrong, hexten.net



More information about the london.pm mailing list