Thread Code in Hash dynamisch erweitern (8 answers)
Opened by bianca at 2012-07-22 19:19

bianca
 2012-07-22 19:19
#160130 #160130
User since
2009-09-13
6978 Artikel
BenutzerIn

user image
Schönen sonnigen guten Abend!
Ich möchte heute gern Code in einem Hash befindlich dynamisch erweitern.
Aber ich weiß nicht, welche Syntax man dazu verwendet.
Hier mal mein Versuch, der natürlich scheitert, weil er naiv ist. Aber ich komme nicht drauf:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#!/usr/bin/perl
use strict;
use warnings;

my %test = (
    code => sub {
        my $input = shift;
        if ($input eq 'h') { return 'hallo'; }
    },
);

$test{code} .= 'if ($input eq \'s\') { return \'sonne\'; }';

my $foo = $test{code}->('h');
print "$foo\n"; # hallo erwartet
$foo = $test{code}->('s');
print "$foo\n"; # sonne erwartet

Quote
Can't use string ("CODE(0x1d1e354)if ($input eq 's'") as a subroutine ref while "strict refs" in use at test.pl line 14.

Bitte um einen Anstoß für die richtige Syntax und bedanke mich.
10 print "Hallo"
20 goto 10

View full thread Code in Hash dynamisch erweitern