use strict and s///ee

K

krichine

Hi!

I have the following:

@::p = (
{ '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 @::p 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 (@::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 (@::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
 
B

Brian McCauley

Hi!

I have the following:

@::p = (

Why do you explicitly place this vaiable in main:: ?
{ '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 @::p rules are more complex than
# shown, in other words, I generally would not be able to code it as a
single expression

So code them as arbitraty Perl fragments. Many of the reasons people
come up with for creating "small languages" turn out in retrospect to
be bogus.
#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 (@::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 (@::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.

Well, yes. You use a bareword string in the expression

${1}.to

For this to work under strictures you'd need to say

${1}."to"

Or just

"$1to"

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?

Just write code.
2. Is there a good way to make the above translation logic work and yet
be able to use strict?

The stict thing is a red herring. It was the use of the unquoted
string 'to' that is the problem not the ${1}.

Please see numerous previous threads and my talk at lightning talk from
YAPC::Europe::2004 (Google this group for it).

Note: I still think you are wrong to go for the small language rather
than just use Perl. (Bare in mind that arbitrary Perl can be embeded
in your small language).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top