IO-ReadPreProcess

 view release on metacpan or  search on metacpan

lib/IO/ReadPreProcess.pm  view on Meta::CPAN

                next;
            }
        }

        if($dir eq 'error') {
            $arg = "Error at $place" if($arg eq '');
            return $self->SetError($arg);
        }

        if($dir eq 'set') {
            return $self->SetError("'$leadin$dir' bad argument '$arg' at $place")
                unless(($arg =~ /^(\w+)=(\d+)/i) and $options{$1});
            $self->{$1} = $2;
            next;
        }

        return $self->SetError("Use of reserved directive '$leadin$dir' at $place", 1)
            if($dir eq 'function' or $dir eq 'do' or $dir eq 'case' or $dir eq 'switch' or $dir eq 'endswitch');

	# User defined sub.
        # At the bottom so cannot redefine an inbuilt directive
        if(exists($self->{subs}->{$dir})) {
            $self->RunSub($dir, $arg);

            next;
        }

        # Should not happen
        return $self->SetError("Unknown directive '$leadin$dir' at $place", 1);
    }
}

# Return the rest of input as an array
sub getlines
{
    my $self = shift;
    my @lines = ();

    return $self->SetError("A file has not been opened", 1)
        unless $self->{Fd};

    return $self->SetError("getlines called in a scalar context", 1)
        unless(wantarray);

    while(my $line = $self->getline) {
        push @lines, $line;
    }

    @lines
}

# Enable the object to be used in the diamond operator:
use overload '<>' => \&getline, fallback => 1;

1;

__END__

=head1 NAME

IO::ReadPreProcess - Macro processing built into IO::File replacement

=head1 SYNOPSIS

    use IO::ReadPreProcess;

    my $fh = new IO::ReadPreProcess(File => './input.file') or
        die "Startup error: $IO::ReadPreProcess::errstr\n";

    while(<$fh>) {
        print $_;    # Or other processing of input
    }

    die($IO::ReadPreProcess::errstr . "\n")
        if($fn->error);

The input file may contain:

    This line will be returned by getline
    .# This is a comment
    .let this := 'that'
    Another line
    .if this eq 'that'
    Another line returned
    .print The variable this has the value \v{this}
    .else
    This line will not be seen
    .fi
    This line returned
    .include another.file
    Line returned after the contents of another.file

=head1 DESCRIPTION

Provide an 'intelligent' bottom end read function for scripts,
what is read is pre-processed before the script sees it.
Your program does not need code to conditionally discard some input, include
files and substitute values.

An easy way of reading input where some lines are read conditionally and other
files included: .if/.else/.elseif/.fi, do: .include .let .print, loops: .while
.for; subroutine definition & call; write to other streams - and more.

Provides IO::Handle-ish functions and input diamond - thus easy to slot in to existing scripts.

The preprocessing layer has variables that can be set and read by your perl code.
In the input files they are set via C<.let> directives, and can be made part of your
script's input with C<.echo> and C<\v{xxx}>.

C<IO::ReadPreProcess> returns lines from the input stream.
This may have directives that include:

=over 4

=item

set variables to arithmetic or string expressions

=item

conditionally return lines



( run in 1.945 second using v1.01-cache-2.11-cpan-13bb782fe5a )