Thread Variable in einem Modul deklarieren. (5 answers)
Opened by Kuerbis at 2018-06-30 14:16

Kuerbis
 2018-06-30 14:16
#188596 #188596
User since
2011-03-20
936 Artikel
BenutzerIn
[default_avatar]
Hallo,

ist es OK in einem Modul eine Variable ($cache hier im Beispiel) zu deklarieren oder hat das irgendwelche bösen Nebenwirkungen:

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
package MyPackage;
use strict;
use Exporter qw( import );
our @EXPORT_OK = qw( print_columns );

sub char_width { ... }

my $cache = [];

sub print_columns {
    my ( $str ) = @_;
    my $width = 0;
    for my $i ( 0 .. ( length( $str ) - 1 ) ) {
        my $c = ord substr $str, $i, 1;
        if ( ! defined $cache->[$c] ) {
            $cache->[$c] = char_width( $c )
        }
        $width = $width + $cache->[$c];
    }
    return $width;
}

1;

View full thread Variable in einem Modul deklarieren.