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

bianca
 2014-09-16 09:29
#177303 #177303
User since
2009-09-13
6977 Artikel
BenutzerIn

user image
Danke euch allen!
Auch danke für die Hinweise, hab sie eingebaut.
Das state ist richtig gut, kannte ich nicht.
Meine Lösung:
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
#!/usr/bin/perl
use strict;
use warnings;
use Crypt::CBC;
use feature 'state';

my $crypt = sub {
    state $c;
    if (!defined $c) {
        print "Debug: erzeuge Objekt\n";
        $c = Crypt::CBC->new(
            -key    => 'foobar',
            -cipher => 'Blowfish',
        );
    }
    return (defined $_[0] && $_[0] ne '' ? $c->encrypt($_[0]) : '');
};

print $crypt->('foo')."\n";
print $crypt->('bar')."\n";
print $crypt->('')."\n";
print $crypt->()."\n";

Das use 5.010; war nur für den Testfall, hab es durch use feature 'state'; ersetzt.
10 print "Hallo"
20 goto 10

View full thread Saubere Perl-Syntax Variable in sub behalten