Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > use strict and s///ee

Reply
Thread Tools

use strict and s///ee

 
 
krichine@juno.com
Guest
Posts: n/a
 
      11-11-2005
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

 
Reply With Quote
 
 
 
 
Brian McCauley
Guest
Posts: n/a
 
      11-12-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi!
>
> I have the following:
>
> @: = (


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 @: 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 (@:) {
> 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.


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).

 
Reply With Quote
 
 
 
 
krichine@juno.com
Guest
Posts: n/a
 
      11-12-2005
> "$1to"

That did it!
Thanks.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When to use "use strict;"? Peng Yu Perl Misc 14 01-27-2010 10:53 PM
How can I use switch -s and 'use strict' at the same time Ting Wang Perl Misc 5 10-06-2005 02:03 PM
why is important to use : use strict? Martina Perl Misc 6 09-20-2005 12:14 AM
Proper way to use an imported constant under 'use strict'? H. Wade Minter Perl Misc 8 04-25-2004 12:58 AM
separate require files for common routines, our, and use strict question Bill Perl Misc 4 01-19-2004 01:39 PM



Advertisments