jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
Re: search and replace using regular expression
by Corion (Patriarch) on Dec 05, 2006 at 10:23 UTC

    That's quite easy. Just ask Perl to tell you what it sees:

    Q:\>perl -w -MO=Deparse -e "s;;new;;old;;g" Unquoted string "old" may clash with future reserved word at -e line 1 +. Unquoted string "g" may clash with future reserved word at -e line 1. Useless use of a constant in void context at -e line 1. Useless use of a constant in void context at -e line 1. BEGIN { $^W = 1; } s//new/; '???'; '???'; -e syntax OK

    So basically, your program is equivalent to $one =~ s//new/;, which replaces the first empty place in the string $one with new, which is what you see.

    Also, this shows again why running with warnings enabled reveals many problematic spots in the code.

Re: search and replace using regular expression
by davorg (Chancellor) on Dec 05, 2006 at 10:27 UTC

    As is often the case, adding "use strict" and "use warnings" gives a clue:

    Bareword "old" not allowed while "strict subs" in use at ./re line 7. Bareword "g" not allowed while "strict subs" in use at ./re line 7.

    B::Deparse is another useful tool.

    $ perl -MO=Deparse re my $one = 'TestnewTest'; $one =~ s//new/; '???'; '???'; print "$one\n"; re syntax OK

    The delimiters for the substitution operator can only be single characters[1]. Your code is therefore being interpreted as:

    $one =~ s//new/; 'old';; 'g';

    So the first empty string Perl finds (at the start of $one) is changed to "new".

    [1] I feel sure that someone will be along shortly to point out exceptions to that rule :-)

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: search and replace using regular expression
by GrandFather (Saint) on Dec 05, 2006 at 21:53 UTC

    jesuashok how many times do we have to tell you! Always, for every Perl script you write, each time you start writing include:

    use strict; use warnings;

    For the code fragment that you give Perl reports:

    Bareword "old" not allowed while "strict subs" in use at noname.pl lin +e 5. Bareword "g" not allowed while "strict subs" in use at noname.pl line +5. Execution of noname.pl aborted due to compilation errors.

    Why do I repeat this when several other people have already said the same thing in this thread? Because you didn't hear and heed the other dozen times you have been told in the past! So, just in case you missed it - always use strictures. Capiche? Comprende? Understand?


    DWIM is Perl's answer to Gödel
Re: search and replace using regular expression
by artist (Parson) on Dec 05, 2006 at 17:25 UTC
    Others have already given answers to your original question. If you want to venture further, there are lots of Regexp modules.
    --Artist