[sf-perl] Perl 5 script "future" revision 1.2

Richard Reina gatorreina at gmail.com
Mon Feb 2 03:30:19 PST 2026


Very interesting. Thank you for explaining.

> 
> On Feb 1, 2026, at 9:53 PM, David Christensen <dpchrist at holgerdanske.com> wrote:
> 
> On 2/1/26 17:53, Richard Reina wrote:
>> What exactly is this code supposed to do?
>>>> On Feb 1, 2026, at 7:32 PM, David Christensen wrote:
>>> <snip>
>>> 
>>> 2026-02-01 17:28:57 root at laalaa ~/sandbox/perl
>>> # cat future
>>> #!/usr/env/perl
>>> # $Id: future,v 1.2 2026/02/02 01:26:20 dpchrist Exp $
>>> # By David Paul Christensen dpchrist at holgerdanske.com
>>> # Public Domain
>>> 
>>> use strict;
>>> use warnings;
>>> 
>>> package Future;
>>> 
>>> sub new
>>> {
>>>    my $class = shift;
>>>    my $rc = shift;
>>>    return bless(sub {return $rc->(@_)}, $class);
>>> }
>>> 
>>> package main;
>>> 
>>> use Test::More;
>>> 
>>> our $FutureClass = "Future";
>>> 
>>> our $v = 'hello, world!';
>>> 
>>> sub future(&)
>>> {
>>>    my $rc = shift;
>>>    return bless(sub {return $rc->(@_)}, $FutureClass);
>>> }
>>> 
>>> my $f = Future->new(sub { $v });
>>> my $g = future { $v };
>>> 
>>> print $f->(), $/;
>>> print $g->(), $/;
>>> 
>>> {
>>>    local $v = 'goodbye, cruel world!';
>>>    print $f->(), $/;
>>>    print $g->(), $/;
>>> }
>>> 
>>> print $f->(), $/;
>>> print $g->(), $/;
>>> 
>>> 
>>> 
>>> 2026-02-01 17:28:59 root at laalaa ~/sandbox/perl
>>> # perl future
>>> hello, world!
>>> hello, world!
>>> goodbye, cruel world!
>>> goodbye, cruel world!
>>> hello, world!
>>> hello, world!
> 
> The code is supposed to test/ demonstrate the following capabilities in Perl 5.  It is a thought exercise, not production quality, and likely contains conceptual and other defects:
> 
> 1.  Class "Future" with a class method (constructor) "new" that accepts a code reference as an argument and returns a copy of that code reference as an object.
> 
> 2.  When the object is invoked, the code reference runs and has access to variables within the caller's lexical scope.
> 
> 3.  If the caller localizes a variable, when invoked the code reference sees the localized value of the variable.
> 
> 4.  When the variable localization goes out of scope, when invoked the code reference sees the previous value of the variable.
> 
> 5.  Similar to the above, but the object is created by a subroutine (factory function) "future" with a prototype of "&" (block) to provide syntactic sugar.  ("future" should be within package "Future", but I was unable to figure out how to get Exporter and "use qw(future)" working.)
> 
> 
> David
> 


More information about the SanFrancisco-pm mailing list