?
Solved

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

Posted on 2006-03-26
3
Medium Priority
?
1,111 Views
Last Modified: 2012-06-22
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'});  
0
Comment
Question by:KFFrench
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
wlfs earned 1000 total points
ID: 16295959
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
 

Author Comment

by:KFFrench
ID: 16296052
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
 
LVL 5

Expert Comment

by:wlfs
ID: 16296588
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question