Wednesday, April 30, 2014

Parsing HTML Pages using HTML::Parser

Introduction to HTML::Parser

There are times when you will need to read an HTML file and extract a field from that file. Perl has a module called HTML::Parser that simplifies this task.

This module reads an HTML file and allows you to define actions when it reads a starting tag, the body and the end tag. To do this, you can define subroutines that are to be executed during these events. The HTML::Parser documentation lists all the events that can happen during processing. For our discussion, we will discuss only the starttext and end events.

Perl Packages

Introduction to Perl Packages

Perl has a feature that allows you to package sets of code into its own package. You use packages to group subroutines so that these can be re-useable or to "isolate" variables used in a subroutine from other subroutines. This isolation of variables is one of the features to implement object oriented programming in Perl.

Thursday, August 8, 2013

Multi-Dimensional Hashes in Perl

Introduction To Hashes

Perl hashes are data types that allow you to associate data to a key. For example, a hash can store article titles with the date of publication as the key:

%hash = (20030227 => 'Multi-dimensional hashes',
         20030113 => 'Introduction to mod_perl',
         20021201 => 'The CPAN Module'

This is a one-dimensional hash.

Wednesday, August 7, 2013

Multi-Dimensional Arrays in Perl

Introduction to Arrays

A Perl array is a data type that allows you to store a list of items. You create them by assigning them to an array variable. The array variable is identified by a @ prefix. To define a list of dates, we do this:

@array = ('20020701', '20020601', '20020501');
This is a one-dimensional array.

Tuesday, August 6, 2013

Formatting Numbers in Perl

Introduction to Formatting Numbers 

Let us say that your program is calculating a number in decimal and you needed to print out leading zeros to it, how would you do it? If there is no need for formatting, you can just use the print command. 

my $a = 10.003403; print "$a";

Regular Expressions: Action, Pattern, Modifier

Introduction to Regular Expressions

In our introductory article on Perl Regular Expressions, we showed how regular expressions can cut down the code that you have to write. This article discusses the format of regular expressions.

Sunday, August 4, 2013

Perl Regular Expressions

Introduction to Regular Expressions

Regular expressions are used to match, change or translate strings against a pattern. In a traditional programming language, you will need to write a routine that scans your string and then do whatever you need to do. This approach is acceptable if the pattern is fairly simple. However, if your pattern is complicated, you will have to write a complicated routine to do your matching and comparing and translating.

Follow by Email