s/(.)/($1)/g vs s/(.)/$to/eg where $to='($1)'

Discussion in 'Perl Misc' started by jilerner@yahoo.com, Apr 12, 2005.

  1. Guest

    I'm looking for the solution with s/from/to/ when the to-part
    comes from ARGV and references the $1. I tried /e and /ee
    but still can't get this right.

    Here's simlpified example. I need to get equivalent
    of s/(.)/($1)/g , the difference being that the (the ($1))
    comes from the variable: $to='($1)';

    $_="abc"; s/(.)/($1)/g; # this does what I want -> "(a)(b)(c)"
    $to='($1)';
    $_="abc"; s/(.)/$to/g; # does not do what I want, I want result to be
    "(a)(b)(c)"
    $_="abc"; s/(.)/$to/ge; # does not do what I want
    $_="abc"; s/(.)/$to/gee; # does not do what I want

    Yakov
     
    , Apr 12, 2005
    #1
    1. Advertising

  2. Steven Kuo Guest

    On 12 Apr 2005 wrote:

    (snipped)
    > I need to get equivalent
    > of s/(.)/($1)/g , the difference being that the (the ($1))
    > comes from the variable: $to='($1)';
    >
    > $_="abc"; s/(.)/($1)/g; # this does what I want -> "(a)(b)(c)"
    > $to='($1)';
    > $_="abc"; s/(.)/$to/g; # does not do what I want, I want result to be
    > "(a)(b)(c)"
    > $_="abc"; s/(.)/$to/ge; # does not do what I want
    > $_="abc"; s/(.)/$to/gee; # does not do what I want





    Here's one way:

    $_ = "abc";
    my $to = '"($1)"';
    s/(.)/$to/eeg;
    print;

    --
    Hope this helps,
    Steven
     
    Steven Kuo, Apr 12, 2005
    #2
    1. Advertising

  3. Guest

    , Apr 12, 2005
    #3
  4. Guest

    Re: s/(.)/($1)/g vs s/(.)/$to/eg where $to='($1)'

    > http://birmingham.pm.org/talks/faq/Hello.html

    Thanks a lot for the link and the answers therein.

    Can you explain what are shortcomings
    of the following solution that you mention ?:

    my $search = 'foo(.*?)bar';
    my $replace = 'wibble$1wobble';

    s/$search/qq!"$replace"!/eeg;
    die $@ if $@;

    Thanks
    Yakov
     
    , Apr 13, 2005
    #4
  5. Re: s/(.)/($1)/g vs s/(.)/$to/eg where $to='($1)'

    <> wrote:
    >> http://birmingham.pm.org/talks/faq/Hello.html

    >
    > Thanks a lot for the link and the answers therein.
    >
    > Can you explain what are shortcomings
    > of the following solution that you mention ?:
    >
    > my $search = 'foo(.*?)bar';
    > my $replace = 'wibble$1wobble';
    >
    > s/$search/qq!"$replace"!/eeg;
    > die $@ if $@;



    * Using eval() on data that comes from outside your program will
    seriously damage your security

    * Do not use it unless the template comes from
    o Within your program
    o A file considered part of your program source from the
    point of view of security
    o A user who has shell access anyhow (not in SUID scripts)

    * You have to be really sure the above will always be true

    my $evil_data='Ha ha ha... @{[ system "rm -rf /" ]}';


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 13, 2005
    #5
  6. Guest

    Re: s/(.)/($1)/g vs s/(.)/$to/eg where $to='($1)'

    wrote:
    > > http://birmingham.pm.org/talks/faq/Hello.html

    >
    > Thanks a lot for the link and the answers therein.
    >
    > Can you explain what are shortcomings
    > of the following solution that you mention ?:
    >
    > my $search = 'foo(.*?)bar';
    > my $replace = 'wibble$1wobble';
    >
    > s/$search/qq!"$replace"!/eeg;
    > die $@ if $@;


    The words I spoke to this slide were something along the lines:

    It has calls eval() inside a loop (s///g is a looping construct) but
    tests $@ outside the loop so will ignore errors from anything but the
    last iteration.

    It uses a single character quoting character (the double quote
    character in this case) so will be confused by that character in
    $replace.

    The solution on the next slide correctly tests $@ inside the loop.

    It uses a multi-character delimiter which is much less likely to
    crop-up by chance.

    Both solutions, of course, suffer from the usual hazards of using
    eval() as described on the slide after.
     
    , Apr 15, 2005
    #6
    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.

Share This Page