[Groningen-pm] Pseudo file example

Johan Vromans jvromans at squirrel.nl
Fri Jan 18 02:57:26 PST 2008


Op verzoek hier het voorbeeld van hoe een iterator of generator te
implementeren als een pseudo-file.

    #!/usr/bin/perl

    use strict;
    use warnings;

    # Create a variable and tie it.
    my $handle;
    tie(*handle, 'CountDown', 12);

    # Get the values.
    while ( <handle> ) {
	print $_ ."\n";
    }

    package CountDown;

    my $start;

    sub TIEHANDLE {
	$start = $_[1];
	bless \my $self, $_[0];
    }

    sub READLINE {
	# Need to return undef since the <handle> loop implicitly
	# tests for defined (see perldoc perlop, I/O Operators).
	return $start ? $start-- : undef;
    }

Groetjes,
	Johan


More information about the Groningen-pm mailing list