develooper Front page | perl.perl6.language | Postings from January 2004

Re: run-once code

Thread Previous | Thread Next
From:
Luke Palmer
Date:
January 14, 2004 10:02
Subject:
Re: run-once code
Message ID:
20040114180218.GA1092@babylonia.flatirons.org
David Storrs writes:
> On Wed, Jan 14, 2004 at 11:57:05AM +0000, Richard Nuttall wrote:
> 
> > How about
> > 
> > $test = sub
> > {
> >     if ( some_expensive_lookup_function() >= $MAX_RECORDS )
> > 
> > 	   mark_that_we_have_reached_max_records();       
> > 
> >        $test = sub{};
> > };
> > 
> > Then call &$test() as needed;
> 
> 
> Neat.  I wouldn't have thought of that; thank you.

Let's not mention that that has way more overhead than a
short-circuiting test, but I guess "the idea's the important thing".

Slight correction here, 

    &$test()

will not call that sub.  Both of:

    $test()
    $test.()

Will, and I think

    &$test.()

Will, but &$test() just refers to the sub object itself.

Which brings up a question:  Can you have a sub reference to a multi?
That is to say, one reference referring to a multiply-dispatched name.
For example:

    multi sub typrint(Int $x) { print "Int $x" }
    multi sub typrint(Str $x) { print "Str $x" }

    my $ref = &typrint;

    $ref.(99);        # Int 99
    $ref.("Hello");   # Str Hello

?

Luke

Luke

> --Dks

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About