use strict; my $fn="myfile"; print readfile($fn); sub readfile { my $out=""; open(FH, $fn) or die "Can't read $fn"; $out .= $_ while(<FH>); return $out; }
I meant to use $fn as passed to the subroutine, but strict didn't help me catch my mistake.
Here is the improved version:
In the improved version, strict catches the mistake. The improvement is to write { } around code outside the subs.use strict; { my $fn="myfile"; print readfile($fn); } sub readfile { my $out=""; open(FH, $fn) or die "Can't read $fn"; $out .= $_ while(<FH>); return $out; }
This helps me with debugging, especially casual programs.
It should work perfectly the first time! - toma
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Improving lexical scoping outside subs
by broquaint (Abbot) on Aug 18, 2003 at 08:51 UTC | |
|
Re: Improving lexical scoping outside subs
by greenFox (Vicar) on Aug 18, 2003 at 08:45 UTC | |
|
Re: Improving lexical scoping outside subs
by dragonchild (Archbishop) on Aug 18, 2003 at 13:20 UTC | |
|
Re: Improving lexical scoping outside subs
by fletcher_the_dog (Friar) on Aug 18, 2003 at 14:33 UTC |