[Omaha.pm] Dumb Questions

Jay Hannah jhannah at omnihotels.com
Thu Jul 28 11:23:42 PDT 2005


> Concerning scope...
> 
> I'm confused about this:
> 
>   my($iR, $iC, $oWkS, $oWkC);
> 
>   foreach my $oWkS (@{$oBook->{Worksheet}}) {
>     for(my $iR = $oWkS->{MinRow} ; defined $oWkS->{MaxRow} && 
> $iR <= $oWkS->{MaxRow} ; $iR++) {
> 
>     ... blah ...
> 
> 
> If I understand this correctly, $iR gets created prior to 
> entering the loop, and then gets ignored by the redefinition 
> in the for loop. So, does this:
> 
>   my($iR, $iC, $oWkS, $oWkC);
> 
>   foreach $oWkS (@{$oBook->{Worksheet}}) {
>     for($iR = $oWkS->{MinRow} ; defined $oWkS->{MaxRow} && 
> $iR <= $oWkS->{MaxRow} ; $iR++) {
>
>     ... blah ...
> 
>
> do essentially the same thing, except not redefine, or is 
> there a reason it's defined twice? 

If $iR is not used anywhere after the foreach block, then you are exactly right.

I think Perl "warnings" would bark at the original, warning about the (probably accidental) re-scoping of $iR. 

You should always use warnings*.

perl -w scriptname.pl 

  or 

#!/usr/bin/perl -w

  or

use warnings;

HTH,

j
* obligatory ubiquitous recommendation




More information about the Omaha-pm mailing list