in reply to Confusion in naming variables in subroutines
For unrelated reasons*, I don't like working at the global level. This has the side-effect of avoiding the issue.
#!/usr/bin/perl -w { my ( $length, $breadth, $height ) = ( 10, 20, 30 ); my $volume = calc_volume( $length, $breadth, $height ); print "\nvolume = $volume\n"; } sub calc_volume { my ( $length, $breadth, $height ) = @_; my $volume = $length * $breadth * $height; return $volume; }
While the snippet you provided has two variables with the same name in scope at the same time, the snippet I provided does not.
* — First, it makes the scope of variables much larger than it needs to be. Secondly, global objects are destroyed in an unpredictable order. That means a child object can be reaped before its owners is, which is bad if you have a custom destructor.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Confusion in naming variables in subroutines
by jbert (Priest) on Jan 24, 2007 at 10:49 UTC |