K
krichine
Hi!
I have the following:
@: = (
{ 'from'=>'^public' }, # if it is
^public, don't change it
{ 'from'=>'^(\w\w_)from', 'to'=>'${1}.to' }, # if it is ^AA_from,
convert it to AA_to
{ 'from'=>'^from', 'to'=> 'to' } # else if it is
^from, convert it to to
);
# first rule above needed to avoid translation "public" if from=='p'
# generally the @: rules are more complex than
# shown, in other words, I generally would not be able to code it as a
single expression
#now fix the patterns with to and from actual values (unknown at
compile time)
my $sch_prefix='p';
my $sch_new_prefix='qr';
for my $p (@:) {
map s/from/$sch_prefix/eg, values %{$p};
map s/to/$sch_new_prefix/eg, values %{$p};
}
# and now process actual string using the above pattern rules, first
match wins
sub translate {
my ($word) = @_;
PATTERN: for my $p (@:) {
if ($word =~ m/$p->{from}/i) {
if (exists $p->{to}) {
$word =~ s/$p->{from}/$p->{to}/eei;
}
last PATTERN;
}
}
return $word;
}
Now, if I use strict;, the above does not work, it gives
"Use of uninitialized value in substitution iterator" and does not
translate as I expect.
If I remove use strict, then translation works as I want.
Questions:
1. Is my mechanism what one would normally do to accomplish this (note
use of ${1}, which I need to preserve the prefix before the match to
from, so that's why I need s//ee, is there a better way?
2. Is there a good way to make the above translation logic work and yet
be able to use strict?
Thanks.
Kirill
I have the following:
@: = (
{ 'from'=>'^public' }, # if it is
^public, don't change it
{ 'from'=>'^(\w\w_)from', 'to'=>'${1}.to' }, # if it is ^AA_from,
convert it to AA_to
{ 'from'=>'^from', 'to'=> 'to' } # else if it is
^from, convert it to to
);
# first rule above needed to avoid translation "public" if from=='p'
# generally the @: rules are more complex than
# shown, in other words, I generally would not be able to code it as a
single expression
#now fix the patterns with to and from actual values (unknown at
compile time)
my $sch_prefix='p';
my $sch_new_prefix='qr';
for my $p (@:) {
map s/from/$sch_prefix/eg, values %{$p};
map s/to/$sch_new_prefix/eg, values %{$p};
}
# and now process actual string using the above pattern rules, first
match wins
sub translate {
my ($word) = @_;
PATTERN: for my $p (@:) {
if ($word =~ m/$p->{from}/i) {
if (exists $p->{to}) {
$word =~ s/$p->{from}/$p->{to}/eei;
}
last PATTERN;
}
}
return $word;
}
Now, if I use strict;, the above does not work, it gives
"Use of uninitialized value in substitution iterator" and does not
translate as I expect.
If I remove use strict, then translation works as I want.
Questions:
1. Is my mechanism what one would normally do to accomplish this (note
use of ${1}, which I need to preserve the prefix before the match to
from, so that's why I need s//ee, is there a better way?
2. Is there a good way to make the above translation logic work and yet
be able to use strict?
Thanks.
Kirill