Home > Net >  evaluate condition assigned to a variable
evaluate condition assigned to a variable

Time:06-18

i am using module Excel::Writer::XLSX and i need to evaluate this condition to check if the sheet was created or not (i need to only execute certain actions if the sheet already exists)

my $sheet = $workbook->add_worksheet($filters{$filter}{description});

how can i do that please ? knowing that if the sheet already exists, the add_worksheet method "dies" with an error like :

Worksheet name 'All Users', with case ignored, is already used. at ./xxxx.pl line 203.

naturlly, doing something like this doesn't work as i believe it evaluates the variable assignment :

if (my $sheet = $workbook->add_worksheet($filters{$filter}{description});) {
   # good, sheet doesn't already exists and was created
   # i can place my code here to insert header rows etc./ colors 
} else {
   # program fires a message like :
   # Worksheet name 'XXXXXX', with case ignored, is already used. at XXX
   # no need to place code for column headers etc., sheet already exists
}

CodePudding user response:

nvm, i used a hash and i'm checking the hash

     if (!$sheets{$filter}) {
        $sheets{$filter} = $workbook->add_worksheet($filters{$filter}{description});

works nicely

CodePudding user response:

You could use

my $name = $filters{$filter}{description};

my $sheet =
   $workbook->get_worksheet_by_name( $name ) ||
   $workbook->add_worksheet( $name );
  •  Tags:  
  • perl
  • Related