use strict and s///ee

Discussion in 'Perl Misc' started by krichine@juno.com, Nov 11, 2005.

  1. Guest

    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
     
    , Nov 11, 2005
    #1
    1. Advertising

  2. wrote:
    > 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).
     
    Brian McCauley, Nov 12, 2005
    #2
    1. Advertising

  3. Guest

    > "$1to"

    That did it!
    Thanks.
     
    , Nov 12, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H. Wade Minter
    Replies:
    8
    Views:
    347
    Robin
    Apr 25, 2004
  2. Martina

    why is important to use : use strict?

    Martina, Sep 19, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    113
    Jürgen Exner
    Sep 20, 2005
  3. Ting Wang
    Replies:
    5
    Views:
    202
    John Bokma
    Oct 6, 2005
  4. Peng Yu

    When to use "use strict;"?

    Peng Yu, Jan 25, 2010, in forum: Perl Misc
    Replies:
    14
    Views:
    190
    John Bokma
    Jan 27, 2010
  5. Marius Gavrilescu

    Re: use strict; use warnings;

    Marius Gavrilescu, Feb 24, 2014, in forum: Perl Misc
    Replies:
    152
    Views:
    606
    $Bill
    Mar 11, 2014
Loading...

Share This Page