Can't call method "Range" on an undefined value

I can't seem to get past this point in my code. For some reason Perl is unable to read some cells of the data in an Excel spreadsheet and returns the error: Can't call method "Range" on an undefined value. Here is the code I'm using:

my $Sheet = $Book->Worksheets ("Data");
my @CF = ($ID, $SO, $LI, $QT, $CR->Date(DATE_SHORTDATE), $CR->Time,
          $MOD->Date(DATE_SHORTDATE), $MOD->Time);
sub GetCF($){
    my ($tgtRows)= @_;
        my $Sheet;
            Return ($Sheet->Range("A".$tgtRows)->{'Value'},
                    $Sheet->Range("L".$tgtRows)->{'Value'},
                    $Sheet->Range("M".$tgtRows)->{'Value'},
                    $Sheet->Range("S".$tgtRows)->{'Value'},
                    $Sheet->Range("V".$tgtRows)->{'Value'},
                    $Sheet->Range("Z".$tgtRows)->{'Value'});  
KFFrenchAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wlfsCommented:
In the code above, perl doesn't even try to read the cells of an Excel sheet :)

In the first line, you set
  my $Sheet = $Book->Worksheets ("Data");
*But* in the sub you have the line:
  my $Sheet;

This line in the sub creates a *new* lexically scoped variable named $Sheet. It is *not* the same as the $Sheet outside of the sub.
The new variable $Sheet in the sub is never assigned to, thus it is undef by default.
Then, $Sheet->Range() triggers the error you described, which is obvious. In the sub $Sheet is undefined, thus you can't call the method Range() on it. It's not an object, it's just undef!

Either delete the line
  my $Sheet;
from the sub competely, or change both declarations of $Sheet to "our $Sheet".
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KFFrenchAuthor Commented:
Ok, so I changed my $Sheet to Our $Sheet in both places and it now says that the subroutine is undefined. I have ialso included the remaing parts of scripts where I try to create the $tmphash.

our $Sheet = $Book->Worksheets ("Data");
my @CF = ($ID, $SO, $LI, $QT, $CR->Date(DATE_SHORTDATE), $CR->Time,
          $MOD->Date(DATE_SHORTDATE), $MOD->Time);
sub GetCF($){
    my ($tgtRows)= @_;
        our $Sheet;
            Return ($Sheet->Range("A".$tgtRows)->{'Value'},
                    $Sheet->Range("L".$tgtRows)->{'Value'},
                    $Sheet->Range("M".$tgtRows)->{'Value'},
                    $Sheet->Range("S".$tgtRows)->{'Value'},
                    $Sheet->Range("V".$tgtRows)->{'Value'},
                    $Sheet->Range("Z".$tgtRows)->{'Value'});

@CF=GetCF($Rows);

while ($Sheet->Range("A". ++$Rows)->{'Value'}){
    my @CF = GetCF($Rows);
    if (!exists $tmphash {$CF[1]}->{$CF[2]}){
        print $CF [1][2];
    }
}
0
wlfsCommented:
Sorry but I don't know the Excel modul at all. So I'm afraid I can't help you any further.
If you don't bother about the points, just post the new problem in a new question. If you do bother, ask in
http://www.experts-exchange.com/Community_Support/
to undo the accept and reopen the question. Other experts won't read this and provide answers as long as the question is marked as accepted.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.