[baltimorepm] Closures and Late-Binding Subroutine Calls

Bernie Simon bsimon at stsci.edu
Thu Apr 26 09:53:48 PDT 2012


Here's an example of what I was getting at. Code modified to work with my
old version of Perl

#!/usr/bin/env perl
use strict;

my $obj = PackageA->new();
my $code = PackageB::configure($obj);
&$code;

package PackageA;
sub new {
    my ($class) = @_;
    # old style objects, avert your eyes!
    my $self = {};
    return bless($self, $class);
}
sub hello {
    print "hello, world\n";
}


package PackageB;
sub configure {
    my ($obj) = @_;
    my $coderef = sub {
        $obj->hello;
    };

    return $coderef;
}



More information about the Baltimore-pm mailing list