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

Hi all,
I am trying to pass a excel worksheet object to a subroutine in perl.
My code is as follows:
my $workbook = Spreadsheet::WriteExcel->new("result.xls"); if ( $somevalue == 1 ) { my $workSheet = $workbook->add_worksheet('PASS'); excel_write ( $workSheet,@pageid_pass,@plot_pass ); }
excel_write is the subroutine which writes the two array's passed to it in a excel file.
The code for excel_write function is as follows...
sub excel_write { my ( $worksheet,@pageid,@plotname ) = @_; $workSheet->write($row,$col,"PLOTNAME",$format); $col++; $workSheet->write($row,$col,"PAGEID",$format); $row++; }
Using this code i am not able to create the excel sheet with data.
Can any one help me in this....
Thanks in advance....
Anand

Replies are listed 'Best First'.
Re: Passing Excel Objects in Perl
by MidLifeXis (Monsignor) on Jul 30, 2008 at 11:17 UTC
    my ( $worksheet,@pageid,@plotname ) = @_;

    ... I do not think that word means what you think it means...

    You may want to dump @pageid and @plotname, and then look at perlref.

    use Data::Dumper; warn Dumper(\@pageid, \@plotname);

    You are passing a scalar and two lists / arrays to your subroutine. Perl will flatten these and you will end up with the first array (@pageid) greedily eating all but the first parameter. To solve this, pass (and receive) the arrays by reference.

    --MidLifeXis


      The array's are passed sucessfully witht the help of my code.

      But I am not able to generate the excel sheet which i am expecting it to generate

      I Want my code to write the values of the array's to be written in two column's of the excel sheet

      In the code, the $worksheet is the object to the Worksheet

      That is not working properly in my code

        Unless the way you are passing the parameters is different from what you have shown, I am not certain that you are getting the data you expect into these variables.

        use strict; use warnings; use Data::Dumper; $Data::Dumper::Indent = 1; sub testme { my ($a, @b, @c) = @_; print Dumper(\$a, \@b, \@c); } print "===== 1 ======\n"; testme("a", qw(b c d), qw(e f g)); print "===== 2 ======\n"; testme("a", ('b', 'c', 'd'), ('e', 'f', 'g')); __DATA__ ===== 1 ====== $VAR1 = \'a'; $VAR2 = [ 'b', 'c', 'd', 'e', 'f', 'g' ]; $VAR3 = []; ===== 2 ====== $VAR1 = \'a'; $VAR2 = [ 'b', 'c', 'd', 'e', 'f', 'g' ]; $VAR3 = [];

        --MidLifeXis

Re: Passing Excel Objects in Perl
by moritz (Cardinal) on Jul 30, 2008 at 11:14 UTC
    Using this code i am not able to create the excel sheet with data.

    So what output do you expect, what do you get? Please read How (Not) To Ask a Question, and provide some more details.

    Are you sure that $somveValue actually is 1, so that excel_write is actually called?

Re: Passing Excel Objects in Perl
by MidLifeXis (Monsignor) on Jul 30, 2008 at 12:35 UTC

    To go further than just passing the variables:

    You are not attempting to write the data at all. Assuming that $row = 0 and $col = 0, $format is defined, and they all are global variables (since they are not defined in excel_write), you are writing the string PLOTNAME to B1, and PAGEID to A2 on the worksheet PASS.

    Do you get errors when running this, or does it run without complaining?

    --MidLifeXis

Re: Passing Excel Objects in Perl
by perlseeker19 (Novice) on Jul 30, 2008 at 12:16 UTC

    hi anand,

    try giving the full path of your excel file,

    for eg $workbook = Spreadsheet::WriteExcel->new('c:\\anand\\result.xls');

      please also check what happens to the value of $row,$column at the first time you invoke the function and for the next time you invoke it.

      the notation actually starts from 0 for the first row and the first column

      if this is the problem then you may make the $row,$column variables global with value 0 and increment them each time you invoke the function

      then reinitialise them to zero at the end of the program.