The Cyberfair::Page Module
Now
that we know how to create our own module,
let’s take a look at Example 16-3. I created
this module to allow me to take some of the subroutines out of the
make_cf.plx
script created for the Main School
CyberFair project, and then share those subroutines across
multiple scripts.
Example 16-3. A Perl module to allow easy re-use of the CyberFair project’s subroutines
package Cyberfair::Page; use strict; BEGIN { use Exporter ( ); use vars qw(@ISA @EXPORT @EXPORT_OK); $VERSION = '0.01'; @ISA = qw(Exporter); @EXPORT = qw( ); @EXPORT_OK = qw(read_page build_page write_page); } sub read_page { # invoked with a full pathname as argument, # returns a hash suitable for # feeding to &build_page my $pathname = shift; my %return_hash; open IN, "$pathname" or die "Couldn't open $pathname for reading: $!"; my $page = join '', <IN>; close IN; return unless $page; if ($page =~ m#<TITLE>(.*)</TITLE>#i) { $return_hash{title} = $1; } while ($page =~ m#<META\s+NAME="([^"]+)"\s+CONTENT="([^"]*)">#gi) { $return_hash{$1} = $2; } if ($page =~ /.+<!--begin content-->\s*(.+?)\s*<!--end content-->/s) { $return_hash{content} = $1; } return %return_hash; } sub build_page { # given a suitable parameter hash, build a CyberFair page # and return it my %param = ( type => 'cf', # these are title => 'Untitled Document', # defaults... description => '', keywords => '', content => '', @_, # supplied name-value pairs come in here ); # translate the various META params into a merged $meta_block ...
Get Perl for Web Site Management now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.