8 Einträge, 1 Seite |
1
2
3
4
5
6
7
8
9
10
11
12
my $foo = ();
my $bar = \$foo;
my $string = 'hello <--REPLACE-->';
# das klappt natürlich nicht
$string =~ s/<--REPLACE-->/$bar/;
$foo = 'world';
print $string, "\n";
$foo = 'community';
print $string, "\n";
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";
$string =~ s/\Q$pat\E/$$ref/g;
$string =~ s/$pat/$$ref/g;
8 Einträge, 1 Seite |