Thread String durch Referenz ersetzen (7 answers)
Opened by bloonix at 2007-02-08 19:50

renee
 2007-02-08 20:31
#74188 #74188
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
So könnte man es realisieren:
Code: (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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/perl

use strict;
use warnings;

package RefReplacer;

use overload '""' => \&replace;

my $self;

sub new{
my ($class,$string) = @_;
$self = {};
bless $self,$class;
$self->string($string);
return $self;
}

sub string{
my ($self,$string) = @_;
$self->{string} = $string if defined $string;
return $self->{string};
}

sub pattern{
my ($self,$pattern,$ref) = @_;
$self->{pattern}->{$pattern} = $ref;
}

sub replace{
my $string = $self->string;
for my $pat(keys %{$self->{pattern}}){
my $ref = $self->{pattern}->{$pat};
$string =~ s/$pat/$$ref/g;
}

return $string;
}

package main;

my $foo = ();
my $bar = \$foo;
my $string = RefReplacer->new('hello <--REPLACE-->');
$string->pattern('<--REPLACE-->',\$foo);

# das klappt natürlich nicht

$foo = 'world';
print $string, "\n";

$foo = 'community';
print $string, "\n";
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/

View full thread String durch Referenz ersetzen