Thread Saubere Perl-Syntax Variable in sub behalten (19 answers)
Opened by bianca at 2014-09-16 08:07

rosti
 2014-09-16 08:37
#177298 #177298
User since
2011-03-19
3194 Artikel
BenutzerIn
[Homepage]
user image
Klarer Fall für state:

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use Crypt::CBC;

my $crypt = sub {
    my $val = shift;
    state %CB;
    my $cb = \%CB;
    
    if (!defined $cb->{cipher}) {
        say "Debug: erzeuge Objekt";
        $cb->{cipher} = Crypt::CBC->new(
            -key    => 'foobar',
            -cipher => 'Blowfish',
        );
    }
    if (defined $val) { return $cb->{cipher}->encrypt($val); }
};


say $crypt->('foo');
say $crypt->('bar');

View full thread Saubere Perl-Syntax Variable in sub behalten