sub make_fruit_counter {
my $fruit = shift;
my $total = 0;
return sub {
my $number = shift;
$total = $total + $number;
return "$total $fruit".($total > 1 ? 's' : '');
};
}
my $apple = make_fruit_counter('apple');
my $orange = make_fruit_counter('orange');
print $apple->(1)."\n";
print $apple->(3)."\n";
print $orange->(1)."\n";
print $apple->(5)."\n";
print $orange->(1)."\n";
####
1 apple
4 apples
1 orange
9 apples
2 oranges
####
#!/usr/bin/perl
use strict;
use warnings;
use Spreadsheet::WriteExcel();
my $book = Spreadsheet::WriteExcel->new('example.xls')
or die "Couldn't create spreadsheet : $!";
my $sheet = $book->add_worksheet('Example sheet');
## Create the title format 'bold'
my $bold = $book->add_format();
$bold->set_bold();
my @months = qw (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my @max_col_width;
my $row = my $col = 0;
## Write titles
$sheet->write( $row, $col++, $_, $bold ) for ('Year',@months);
## Move to beginning of next row
$col = 0;
$row++;
## Write arbitrary values
my $val = 0;
for my $year ( 2003 .. 2007 ) {
## Decide whether to increase the max width for the year column
my $width = length($val);
$max_col_width[$col] = $width
if !defined $max_col_width[$col]
|| $width > $max_col_width[$col];
$sheet->write($row,$col++,$year);
for ( 1..@months) {
## Decide whether to increase the max width for this column
my $width = length($val);
$max_col_width[$col] = $width
if !defined $max_col_width[$col]
|| $width > $max_col_width[$col];
$sheet->write( $row, $col++, $val );
$val = $val + 50;
}
## Move to the beginning of the next row
$row++;
$col = 0;
}
## Set the width of each column to the width of the longest value
foreach my $column (0..$#max_col_width) {
$sheet->set_column($column,$column,$max_col_width[$column]);
}
$book->close
or die "Couldn't close spreadsheet : $!";
####
#!/usr/bin/perl
use strict;
use warnings;
use Spreadsheet::WriteExcel();
my $book = Spreadsheet::WriteExcel->new('example.xls')
or die "Couldn't create spreadsheet : $!";
my $bold = $book->add_format();
$bold->set_bold();
## Create an anonymous sub for writing to a new worksheet
my $writer = make_writer($book->add_worksheet('Example sheet'));
my @months = qw (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
$writer->( 'write', $_, $bold ) for ('Year',@months);
$writer->('next_row');
my $val = 0;
for my $year (2003..2007 ) {
$writer->('write',$year);
for (1..@months) {
$writer->( 'write', $val );
$val = $val + 50;
}
$writer->('next_row');
}
$writer->('set_col_widths');
$book->close
or die "Couldn't close spreadsheet : $!";
#===================================
sub make_writer {
#===================================
## These lexical variables will be remembered inside the anonymous sub
my $sheet = shift;
my $col = 0;
my $row = 0;
my @max_col_width;
return sub {
my $action = shift;
## Write to the current cell and move to the next cell
if ( $action eq 'write' ) {
my ( $value, $format ) = @_;
## Set the max_col_width
my $width = length($value);
$max_col_width[$col] = $width
if !defined $max_col_width[$col]
|| $width > $max_col_width[$col];
$sheet->write( $row, $col, $value, $format );
return $col++;
}
## Move to the beginning of the next row
elsif ( $action eq 'next_row' ) {
$row++;
my $orig_col = $col;
$col = 0;
return $orig_col;
}
## Set the column widths to the width of the widest value
elsif ( $action eq 'set_col_widths' ) {
for ( my $col = 0; $col < @max_col_width; $col++ ) {
$sheet->set_column( $col, $col, $max_col_width[$col] );
}
return;
}
$action ||= '';
die "Unknown action '$action'";
};
}
####
#!/usr/bin/perl
use strict;
use warnings;
use Spreadsheet::WriteExcel();
my $book = Spreadsheet::WriteExcel->new('example.xls')
or die "Couldn't create spreadsheet : $!";
my $bold = $book->add_format();
$bold->set_bold();
my @months = qw (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
## Write titles for the company spreadsheet
my $company = make_writer($book->add_worksheet('Company'));
$company->('write',$_,$bold) for ('Department','Year',@months);
$company->('next_row');
my $val = 0;
for my $dept_name qw (North South West East) {
## Create a new worksheet for this department and add titles
my $dept = make_writer($book->add_worksheet($dept_name));
$dept->('write',$_,$bold) for ('Year',@months);
$dept->('next_row');
for my $year (2003..2007 ) {
# The company sheet has the extra column "Department"
$company->('write',$dept_name);
$company->('write',$year);
$dept->('write',$year);
for (1..@months) {
$dept->( 'write', $val );
$company->( 'write', $val );
$val = $val + 50;
}
$dept->('next_row');
$company->('next_row');
}
$dept->('set_col_widths');
}
$company->('set_col_widths');
$book->close
or die "Couldn't close spreadsheet : $!";