last statement evaluate to true ✦ Put the file somewhere perl will find it #!/usr/bin/perl use strict; use warnings; print "Hello Modules\n"; 1; Monday, December 12, 2011
just perl code ✦ It is included in the app by a “use” directive ✦ The module is loaded and executed before anything else happens (at compilation) Monday, December 12, 2011
hit_plants { secret_weapon(); } 1; sub secret_weapon { print("Plants Win\n") } sub hit_aliens { secret_weapon(); } 1; use Plants; use Aliens; print "Hitting the aliens\n"; hit_aliens(); Monday, December 12, 2011
✦ It is a lexical definition, valid until the end of scope ✦ All global subroutines and our variables are actually packaged scope package Aliens; use strict; sub secret_weapon { print("Aliens win\n") } sub hit_plants { secret_weapon(); } 1; Monday, December 12, 2011
we need to prefix it with the package name. ✦ The default package is called main use strict; use warnings; use AliensPkg; Aliens::hit_plants(); Monday, December 12, 2011
first letter ✦ Usually the same as the name of the file ✦ Ends with true value package ModuleName; use strict; use warnings; # Subroutines declarations go here 1; Monday, December 12, 2011
Normal text can appear anywhere in the pod ✦ All pod is written after the 1; (end of the module) ✦ Full code: Calc.pm 1; =head1 Simple Calculator Module The calculator module provides functionality related to calculating and doing math. It supports all basic operations. Monday, December 12, 2011
NAME ModuleName is a cool module to do the job =head1 DESCRIPTION =over =item func1(x, y) =item func2(x, y, z) =back =head1 SEE ALSO =head1 BUGS Monday, December 12, 2011
declared in ✦ Scopes: block, eval, file use strict; use warnings; foreach my $number (1..10) { # OK print $number, "\n" } # Error print $number Monday, December 12, 2011
5 ✦ X is lexical, package keyword affects package variables use strict; use warnings; package Foo; my $x = 5; package main; print "x = $x\n"; Monday, December 12, 2011
the delimiter that will be printed when writing an array inside quotes ✦ Setting it locally changes its value only inside the block use strict; use warnings; my @l = (1, 2, 3); { local $"=")("; print "(@l)\n"; } print "(@l)\n"; Monday, December 12, 2011
possible to set or remove global package names by changing a hash ✦ This can be used to create subroutines dynamically ✦ Demo Monday, December 12, 2011
always consider YAML instead ✦ Use ‘require’ for dynamic or conditioned loading ✦ Use ‘use’ for dependencies ✦ Most of the time, use use Monday, December 12, 2011
modules ✦ %INC holds all the loaded modules ✦ change value of @INC using: use lib <newdir> ✦ Can use PERL5LIB environment variable Monday, December 12, 2011
easier ✦ It allows exporting of functionality from one module to another ✦ Note: If using exporter, module name and file name must be the same Monday, December 12, 2011
keywords ✦ Use $_ for the error use strict; use warnings; use Try::Tiny; try { die 'FileError'; } catch { warn 'file error' if /FileError/; } Monday, December 12, 2011