Perl, "closure" using Hash

Posted by Mike on Stack Overflow See other posts from Stack Overflow or by Mike
Published on 2010-06-15T13:13:52Z Indexed on 2010/06/15 13:22 UTC
Read the original article Hit count: 151

Filed under:
|

I would like to have a subroutine as a member of a hash which is able to have access to other hash members.

For example

sub setup {
  %a = (
   txt => "hello world",
   print_hello => sub {
    print ${txt};
  })
return %a
}

my %obj = setup();
$obj{print_hello};

Ideally this would output "hello world"

© Stack Overflow or respective owner

Related posts about perl

Related posts about closure