=head1 TITLE

Immediate subroutines

=head1 VERSION

  Maintainer: Jean-Louis Leroy
  Date: 04 Aug 2000
  Version: 1
  Mailing List: [EMAIL PROTECTED]
  Number: 18

=head1 ABSTRACT

This very simple construct, inspired by the Forth language,  makes the
parser extensible by Perl code, providing powerful macro capabilities,
multi-line comments, inline functions and conditional compilation.

=head1 DESCRIPTION

When the parser sees a subroutine that has been marked as 'immediate',
it calls it immediately. The call's arguments are implicitly quoted as
with q{} and the resulting strings are passed to the subroutine. The
entire call is removed from the parse stream and replaced with the
subroutine's return value.

=head1 SYNTAX

use immediate qw( compileif ); # mark subroutines as immediate

=head1 EXAMPLES

# multiline comments

sub comment
{
        return '';
}

use immediate 'comment';

sub foo
{
        # ...
        comment {
                this is a multiline comment;
                the call to comment is executed at parse time
                and returns an empty string that replaces
                the whole call in the parse stream };
}

# conditional compilation

sub compileif
{
        my ($condition, $body) = @_;
        return eval($condition) ? $body : '';
}

use immediate qw( compileif ); # mark subroutines as immediate

sub bar
{
        compileif -e 'state'
                {
                do 'state';
                }

        compileif $Module::VERSION > 1.23,
                {
                # blah blah blah
                }
}

# macros

sub square
{
        my $arg = shift;
        my $gensym = $arg . '_';
        $gensym .= '_' while $arg =~ /$gensym/;
        return "do { $gensym = $arg; $gensym * $gensym }";
}

=head1 IMPLEMENTATION

A flag is associated with the data structure associated to a
subroutine by the parser. The pragmatic module 'immediate' is used to
turn the flag on. When the parser recognizes a subroutine call, it
checks the flag and if it's true, proceeds as described above.

=head1 REFERENCES

The Forth standard.
"Starting Forth", by Leo Brodie

Reply via email to