http://qs321.pair.com?node_id=676416

lepetitalbert has asked for the wisdom of the Perl Monks concerning the following question:

dear Monks,

After a couple of hours reading stuff about 'bless' due to a

Can't call method "Workbooks" on unblessed reference

I still don't understand.

the error come from the last line :

my $excel_template = $file_sys_path . $invoice_page_name; my $Excel = Win32::OLE->GetActiveObject('Excel.Application') or Win32: +:OLE->new('Excel.Application', 'Quit') or die "Could not start excel" +; $Excel->{'Visible'} = 0; $Excel->{DisplayAlerts} = 0; my $book = $Excel->Workbooks->Open( $excel_template ) or die ; # open +Excel file

I have no idea about what I have to bless : $book, $Excel or $excel_template.

Any hint is welcome

Have a nice day

"There is only one good, namely knowledge, and only one evil, namely ignorance." Socrates

Replies are listed 'Best First'.
Re: don't understand bless
by ides (Deacon) on Mar 26, 2008 at 15:29 UTC

    You don't need to bless anything most likely. That error is telling you that $Excel doesn't contain an object and so you can't call a method on it.

    bless() is used in creating objects, so to make the error message make more sense think of it as "Can't call method Workbook on a non-object"

    Frank Wiles <frank@revsys.com>
    www.revsys.com

      Hello dear Monks,

      Thank you all for your help

      This seems to be second strange bug I'm facing with perl / apache / excel.

      this morning I went back to the code which first gave me error :

      my $Excel = Win32::OLE->new('Excel.Application') or die "Could not + start excel"; $Excel->{'Visible'} = 0; $Excel->{DisplayAlerts} = 0; my $Book = $Excel->Workbooks->Open ( $excel_template ) or die "Could not open excel template $excel_template"; # open + Excel file my $Sheet = $Book->Worksheets(1) or die "Could not get Worksheet i +n $excel_template"; $Sheet->Cells( 9 , 7 )->{Value} = "$name";

      and it works again !?

      If anyone has an idea where to search ? apache / perl / OLE / excel ?

      Have a nice day

      "There is only one good, namely knowledge, and only one evil, namely ignorance." Socrates
Re: don't understand bless
by pc88mxer (Vicar) on Mar 26, 2008 at 15:23 UTC
    I would check the value of $Excel to see if it is what you think it is. From the perlop man page:
    $a = $b or $c; # bug: this is wrong ($a = $b) or $c; # really means this $a = $b || $c; # better written this way

      I think your suggestion to check the value of $Excel is a good one. However, the reference to perlop doesn't seem relevant here.

      I'm guessing you're talking about this bit:

      my $Excel = Win32::OLE->GetActiveObject('Excel.Application') or Win32::OLE->new('Excel.Application', 'Quit') or die "Could not start excel";

      This doesn't look like a $x = $y || $z assignment to me. It looks as if it's trying to GetActiveObject(), and if that fails, try to quit Excel. If that fails, just die.

      Now that I look at it, the problem may be that GetActiveObject() fails, quitting Excel succeeds, and the script then doesn't die. It goes on to try to use (the bogus) $Excel anyway. So I'd try this:

      my $Excel = Win32::OLE->GetActiveObject('Excel.Application'); if ( ! $Excel ) { Win32::OLE->new('Excel.Application', 'Quit'); die 'Could not start Excel'; }

        If using or was truly desired,

        my $Excel = Win32::OLE->GetActiveObject('Excel.Application') or do { Win32::OLE->new('Excel.Application', 'Quit'); die "Could not start excel"; };
Re: don't understand bless
by superfrink (Curate) on Mar 26, 2008 at 15:27 UTC
    Can't call method "Workbooks" on unblessed reference ... my $book = $Excel->Workbooks->Open( $excel_template ) or die ; # open +Excel file
    The code posted is trying to run the Workbooks method of the $Excel variable. In Object-Oriented speak a "method" is a subroutine tied to an object or class.

    To get started with objects in perl see bless, perltoot, and perlobj.