Automatically call hash values ​​that are routines

I have a hash with several values ​​that are not scalar data, but rather anonymous routines that return scalar data. I want to make this completely transparent to the part of the code that looks for values ​​in the hash, so that it does not need to know that some of the hash values ​​may be anonymous routines that return scalar data, and not just old scalar data.

To do this, is there a way for anonymous routines to be executed when accessing their keys without using special syntax? Here is a simplified example illustrating the purpose and problem:

#!/usr/bin/perl

my %hash = (
    key1 => "value1",
    key2 => sub {
        return "value2"; # In the real code, this value can differ
    },
);

foreach my $key (sort keys %hash) {
    print $hash{$key} . "\n";
}

The output I need is:

perl ./test.pl
value1
value2

Instead, I get:

perl ./test.pl
value1
CODE(0x7fb30282cfe0)
+4
7

, "", .

, .

  • -, .
  • tie - , .

, :

use Time::HiRes     qw( time );
use Variable::Magic qw( cast wizard );

{
   my $wiz = wizard(
      data => sub { my $code = $_[1]; $code },
      get => sub { ${ $_[0] } = $_[1]->(); },
   );

   sub make_evaluator { cast($_[0], $wiz, $_[1]) }
}

my %hash;
$hash{key1} = 'value1';
make_evaluator($hash{key2}, sub { 'value2@'.time });

print("$hash{$_}\n") for qw( key1 key2 key2 );

:

value1
value2@1462548850.76715
value2@1462548850.76721

:

my %hash; make_evaluator($hash{key}, sub { ... });
my $hash; make_evaluator($hash->{$key}, sub { ... });

my $x; make_evaluator($x, sub { ... });
make_evaluator(my $x, sub { ... });

make_evaluator(..., sub { ... });
make_evaluator(..., \&some_sub);

"" . -

my $hoh = {
   { 
      key1 => 'value1',
      key2 => sub { ... },
      ...
   },
   ...
);

for my $h (values(%$hoh)) {
   for my $v (values(%$h)) {
      if (ref($v) eq 'CODE') {
         make_evaluator($v, $v);
      }
   }
}
+2

, , , tie, . , . , , 99% .

, , , , , , :

sub evaluate {
    my $val = shift;
    return $val->() if ref($val) eq 'CODE';
    return $val;  # otherwise
}

:

foreach my $key (sort keys %hash) {
    print evaluate($hash{$key}) . "\n";
}
+5

, , tie, . ,

tie, Tie::StdHash

Tie::StdHash, . , , ,

TIEHASH, , tie, FETCH, FETCH, , , ,

, , . , , , -. -

SpecialHash.pm

package SpecialHash;

use Tie::Hash;
use base 'Tie::StdHash';

sub TIEHASH {
    my $class = shift;
    bless { @_ }, $class;
}

sub FETCH {
    my $self = shift;
    my $val = $self->SUPER::FETCH(@_);
    ref $val eq 'CODE' ? $val->() : $val;
}

1;

main.pl

use strict;
use warnings 'all';

use SpecialHash;

tie my %hash, SpecialHash => (
    key1 => "value1",
    key2 => sub {
        return "value2"; # In the real code, this value can differ
    },
);

print "$hash{$_}\n" for sort keys %hash;

value1
value2



Update

, , .

my %hash = (
    a => {
        key_a1 => 'value_a1',
        key_a2 => sub { 'value_a2' },
    },
    b => {
        key_b1 => sub { 'value_b1' },
        key_b2 => 'value_b2',
    },
);

tie , , , , . , TIEHASH SpecialHash, tie

, tie ,

%hash, -.

tie %$val, SpecialHash => ( %$val )

tie my %hash, SpecialHash => ( ... )

, $val, , .

, %hash, ,

use strict;
use warnings 'all';
use SpecialHash;

my %hash = (
    a => {
        key_a1 => 'value_a1',
        key_a2 => sub { 'value_a2' },
    },
    b => {
        key_b1 => sub { 'value_b1' },
        key_b2 => 'value_b2',
    },
);

# Tie all the secondary hashes that are hash references
#
for my $val ( values %hash ) {
    tie %$val, SpecialHash => ( %$val ) if ref $val eq 'HASH';
}

# Dump all the elements of the second-level hashes
#
for my $k ( sort keys %hash ) {

    my $v = $hash{$k};
    next unless ref $v eq 'HASH';

    print "$k =>\n";

    for my $kk ( sort keys %$v ) {
        my $vv = $v->{$kk};
        print "    $kk => $v->{$kk}\n" 
    }
}

a =>
    key_a1 => value_a1
    key_a2 => value_a2
b =>
    key_b1 => value_b1
    key_b2 => value_b2
+4

, . tie , coderefs , overload ed mehods , , .

+2

perl - tie. - .

, , .

:

#!/usr/bin/env perl

package RandomScalar;

my $random_range = 10;

sub TIESCALAR {
    my ( $class, $range ) = @_;
    my $value = 0;
    bless \$value, $class;
}

sub FETCH {
    my ($self) = @_;
    return rand($random_range);
}

sub STORE {
    my ( $self, $range ) = @_;
    $random_range = $range;
}

package main;

use strict;
use warnings;

tie my $random_var, 'RandomScalar', 5;

for ( 1 .. 10 ) {
    print $random_var, "\n";
}

$random_var = 100;
for ( 1 .. 10 ) {
    print $random_var, "\n";
}

- "" . hash - .

- , . , $random_var , , "".

, , , , .

- "" :

#!/usr/bin/env perl

package MagicHash;

sub TIEHASH {
    my ($class) = @_;
    my $self = {};
    return bless $self, $class;
}

sub FETCH {
    my ( $self, $key ) = @_;
    if ( ref( $self->{$key} ) eq 'CODE' ) {
        return $self->{$key}->();
    }
    else {
        return $self->{$key};
    }
}

sub STORE {
    my ( $self, $key, $value ) = @_;
    $self->{$key} = $value;
}

sub CLEAR {
    my ($self) = @_;
    $self = {};
}

sub FIRSTKEY {
    my ($self) = @_;
    my $null = keys %$self;    #reset iterator
    return each %$self;
}

sub NEXTKEY {
    my ($self) = @_;
    return each %$self;
}

package main;

use strict;
use warnings;
use Data::Dumper;

tie my %magic_hash, 'MagicHash';
%magic_hash = (
    key1 => 2,
    key2 => sub { return "beefcake" },
);

$magic_hash{random} = sub { return rand 10 };

foreach my $key ( keys %magic_hash ) {
    print "$key => $magic_hash{$key}\n";
}
foreach my $key ( keys %magic_hash ) {
    print "$key => $magic_hash{$key}\n";
}
foreach my $key ( keys %magic_hash ) {
    print "$key => $magic_hash{$key}\n";
}

, "" . eval , - .

"" - - " ", ... , , , tie. , . ( , , , , " " ).

+2

, ref, :

foreach my $key (sort keys %hash) {
    if (ref $hash{$key} eq 'CODE'){
        print $hash{$key}->() . "\n";
    }
    else {
        print "$hash{$key}\n";
    }
}

, - ( ) , , non-coderefs, , refs.

, , - , . sub .

key2 => sub {
    return "value2";
}->(),
+1

, - . , . , , , . .

, ,

my %hash = (
    key1 => sub { return "value1" },
    key2 => sub {
        # carry on some processing ...
        return "value2"; # In the real code, this value can differ
    },
);

print $hash{$_}->() . "\n" for sort keys %hash;

, , .

0

All Articles