to any perl data type • Note & means subroutine # create a list ref with \@ my $l_ref = \@l; # create a hash ref with \% my $h_ref = \%h; # create a scalar ref with \$ my $x_ref = \$x; # create a subroutine ref with \& my $f_ref = \&foo; Tuesday, July 10, 12
to get the original value • This is called “dereference” # note that assignment operator # generates a copy operation my @copy = @$l_ref; my %copy = %$h_ref; my $copy = $$x_ref; my $res = &$f_ref; Tuesday, July 10, 12
operator • Note the different types of parens # Direct element access using # references my $first = $l_ref->[0]; my $val = $h_ref->{key}; my $result = $f_ref->('x', 'y'); Tuesday, July 10, 12
returns sum(@first) - sum(@second). Use references to pass the data • Write a function that takes a hash ref, key and value and inserts the new (key, value) to the hash Tuesday, July 10, 12
$filename ) - returns a single scalar with all contents of the file • write_file( $filename, $content ) - takes a scalar and write it to a file Tuesday, July 10, 12
a complex data structure • Don’t forget to print it use v5.14; use Data::Dumper; my @letters = ('a'..'f'); my @numbers = (0..7); my %contacts = ( Mike => 7, Jim => 5 ); my @data = (\@letters, \@numbers, \%contacts); print Dumper(\@data); Tuesday, July 10, 12
a reference • Print in colors use v5.14; use Data::Printer; my @letters = ('a'..'f'); my @numbers = (0..7); my %contacts = ( Mike => 7, Jim => 5 ); my @data = (\@letters, \@numbers, \%contacts); p @data; Tuesday, July 10, 12
Each workbook “has” an array of work sheets use Spreadsheet::ParseExcel; use strict; use warnings; use Data::Printer; my $parser = Spreadsheet::ParseExcel->new; my $workbook = $parser->parse('demo1.xls') or die $parser->error; p $workbook; Tuesday, July 10, 12
my $workbook = Spreadsheet::WriteExcel->new('writer1.xls'); my $ws = $workbook->add_worksheet(); my @data = qw/red blue green white cyan magenta black/; while ( my ($idx, $val) = each @data ) { my $fmt = $workbook->add_format; $fmt->set_color( $val ); $ws->write( $idx, 0, $val, $fmt ); } Tuesday, July 10, 12
title => 'This is an example spreadsheet', subject => 'With document properties', author => 'John McNamara', comments => 'Created with Perl and Spreadsheet::WriteExcel', ); Tuesday, July 10, 12
in current directory and creates an excel sheet including: file name, file size, modification date • Use red color for files larger than 2K • Create a textual report of the data Tuesday, July 10, 12
the full list use English qw/-no_match_vars/; my $filename = $PROGRAM_NAME; open my $fh, "<", $filename or die $OS_ERROR; while (<$fh>) { print; } close $fh; Tuesday, July 10, 12
stored in $_ • Don’t forget to end with semicolon use strict; use warnings; use Try::Tiny; try { die "good bye cruel world"; } # catch is optional catch { warn "Exception: $_"; }; Tuesday, July 10, 12
output/ errors • Use tee to run code normally AND get its output/errors use v5.14; use Capture::Tiny qw/:all/; sub do_stuff { say "Hello World"; warn "3... 2.... 1...."; return 7; } # Put "Hello WOrld" in $stdout # "3... 2... 1..." in $stderr # 7 in @result my ($stdout, $stderr, @result) = capture { do_stuff; }; # Same, but also print output my ($t_stdout, $t_stderr, @t_result) = tee { do_stuff; }; Tuesday, July 10, 12
known exe paths • Works on all platforms use v5.14; use File::Which; my $perldoc = which('perldoc'); say "perldoc is at: $perldoc"; Tuesday, July 10, 12
your own • Provides: copy, move • Return 1 on success, 0 on failure use v5.14; use File::Copy; my $program_name = $0; copy($program_name, "copy1.pl"); copy($program_name, "copy2.pl"); move("copy2.pl", "copy3.pl"); Tuesday, July 10, 12
files you want, and get a list of files matching the attributes use v5.14; use Data::Printer; use File::Find::Declare; my $finder = File::Find::Declare->new({ dirs => '/home', ext => '.mp3', recurse => 1, }); my @music = $finder->find; p @music; Tuesday, July 10, 12
a file size and search for all files larger than that size • Use IO::Interactive to exit immediately if called as non-interactive • Use Term::ProgressBar::Simple to draw a progress bar Tuesday, July 10, 12
dest_name, reverse • Write a perl that reads the file, if reverse is 0 simply copy from source_name to dest_name • If reverse is 1 copy in reverse order of lines Tuesday, July 10, 12