parametrical substitution in perl

Discussion in 'Perl Misc' started by ari, Oct 31, 2005.

  1. ari

    ari Guest

    I want to write a subroutine which performs string substitution
    parametrically, as follows:

    sub doit()
    {
    my ($in,$before,$after)=@_;
    my $out;
    $out = $in;
    $out =~ s/$before/$after/e;
    return $out;
    }

    In general this works fine, but if I try and use captured buffers, it
    doesn't work. For example, I want to do the following:

    s/^(...)/$1 /

    i.e. insert a blank after the first three chars.

    if I do:

    &doit("ABCDEF","^(...)",'$1 ');

    I get back "$1 DEF"

    Does anyone know how to make this work???
     
    ari, Oct 31, 2005
    #1
    1. Advertising

  2. ari wrote:
    > I want to write a subroutine which performs string substitution
    > parametrically, as follows:
    >
    > sub doit()

    ----------^^
    Why?

    > {
    > my ($in,$before,$after)=@_;
    > my $out;
    > $out = $in;
    > $out =~ s/$before/$after/e;
    > return $out;
    > }
    >
    > In general this works fine, but if I try and use captured buffers, it
    > doesn't work. For example, I want to do the following:
    >
    > s/^(...)/$1 /
    >
    > i.e. insert a blank after the first three chars.
    >
    > if I do:
    >
    > &doit("ABCDEF","^(...)",'$1 ');

    --^
    Why?

    > I get back "$1 DEF"
    >
    > Does anyone know how to make this work???


    sub doit {
    my ($in,$before,$after)=@_;
    my $out = $in;
    $out =~ s/$before/$after/ee;
    return $out;
    }

    doit('ABCDEF', '^(...)', '"$1 "');

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Oct 31, 2005
    #2
    1. Advertising

  3. ari

    Paul Lalli Guest

    ari wrote:
    > I want to write a subroutine which performs string substitution
    > parametrically, as follows:
    >
    > sub doit()

    ^^
    Here, you're telling Perl that no parameter will be passed to this
    subroutine.

    > {
    > my ($in,$before,$after)=@_;


    .... yet, here, you're expecting three parameters to be passed to this
    subroutine. This should be an error.

    Except...

    > my $out;
    > $out = $in;
    > $out =~ s/$before/$after/e;
    > return $out;
    > }
    >
    > In general this works fine, but if I try and use captured buffers, it
    > doesn't work. For example, I want to do the following:
    >
    > s/^(...)/$1 /
    >
    > i.e. insert a blank after the first three chars.
    >
    > if I do:
    >
    > &doit("ABCDEF","^(...)",'$1 ');

    ^^^
    Here, you're telling Perl to ignore what you previously told it about
    the subroutine. Why are you doing this?

    > I get back "$1 DEF"
    >
    > Does anyone know how to make this work???


    This topic came up in this group about a month ago:
    http://groups.google.com/group/comp..._frm/thread/2a47b0f6f4ccbe2e/86a1fff0a9196208
    The end result was that you need a double-eval on the s/// (ie, add an
    additional /e modifier), and you need the replacement argument to be
    whatever you want inside of double quotes.

    doit("ABCDEF, "^(...)", q{"$1 "});
    ....
    $out =~ s/$before/$after/ee;

    See also: perldoc -q expand
    (but note that the last time this came up, there was some controversy
    as to the "goodness" of that FAQ answer).

    Paul Lalli
     
    Paul Lalli, Oct 31, 2005
    #3
  4. ari

    ari Guest

    Paul -

    You are right on both counts - my bad coding (the "()" after doit was a
    mistake - although perl didn't seem to mind very much....

    More importantly, your solution works, so much thanks!

    Ari
     
    ari, Nov 1, 2005
    #4
    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. Tyson Marchuk
    Replies:
    7
    Views:
    141
    Brian McCauley
    Feb 24, 2004
  2. kris

    Perl Substitution

    kris, Apr 28, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    112
  3. kp

    Command substitution in perl

    kp, Oct 7, 2006, in forum: Perl Misc
    Replies:
    5
    Views:
    823
  4. Replies:
    3
    Views:
    135
    Dr.Ruud
    Nov 22, 2006
  5. substitution in perl

    , Feb 1, 2007, in forum: Perl Misc
    Replies:
    1
    Views:
    111
    Mirco Wahab
    Feb 1, 2007
Loading...

Share This Page