in reply to expressions as constants

Hi!

What you need is closure. Closures are little anonymous functions that you can assign to a scalar and call repeatedly. You do that like so:

my $last_in_sentence = sub {return $content_array[$loc] =~ m/\.$/}; [...] if (&$last_in_sentence) { [...] }

Voila! Hope that helps.

--j

Replies are listed 'Best First'.
Re^2: expressions as constants
by revdiablo (Prior) on Jul 15, 2004 at 18:20 UTC
    Closures are little anonymous functions that you can assign to a scalar and call repeatedly

    Not to pick nits, but that's not exactly what a closure is. What you've described is just a standard anonymous subroutine. A closure is a bit more than that. A closure captures any surrounding lexical variables, and keeps their values between calls to the subroutine. Here's the canonical example:

    sub makecounter { my $count = 0; return sub { ++$count }; } my $counter1 = makecounter; my $counter2 = makecounter; for (1 .. 5) { print $counter1->(), "\n"; } print $counter2->(), "\n";

    Update: another small bit of clarification. Anonymous subroutines are not necessary for creating closures. Regular subroutines will cause closure around any surrounding lexical variables, too. It's not as flexible, since you only get one closed set of lexical variables, but it works the same. Here's an example:

    { # create a scope for the closure my $count = 0; sub counter { ++$count } } for (1 .. 5) { print counter, "\n"; }