The Iterator Design Pattern
Short Description
Iterators as a design pattern are much more apparent as a pattern in languages …… The Iterator design pattern has three parts: the data, the iterator, …
Website: www.theperlreview.com | Filesize: 410kb
Content
The Iterator Design Pattern
brian d foy, comdog@panix.com
Abstract
The Iterator pattern separates the details of traversing a collection so that they can vary independently.
Perl provides some of these parts already, although in some cases I need to provide my own
implementations of them.
1 Introduction
Iterators as a design pattern are much more apparent as a pattern in languages that do not have aggregates
as first class objects. Perl has lists, along with the variable types arrays and hashes, and it is trivial to go
through any of these with built-in Perl structures like foreach, each, map, and so on. The file input operator,
<>, iterates through the lines of the file. The readdir() and glob() functions iterate through filenames. In
scalar context, the match operator with the global flag, m//g, iterates through matches.
In Perl I do not have to write classes to traverse an array, but I may need to write code to go through my
own complex data structures because Perl’s built-in control structures, like foreach(), map(), and grep(), do
not define an interface that objects can use to control the iteration. They take a list, and I have to…
Get the file Download here
Related Books:Related Searches: iterator pattern, input operator, global flag, scalar context, variable types
Comments
Leave a Reply