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

Okay guys. I seem to be handling the exercises from the llama with varying degrees of success. But as soon as I try a 'real life application of Perl, I just lose it!

I tried writing this simple little script to remove the e-mail quoting ('>') from messages.

This is what I came up with.
#!/usr/bin/perl use strict; use warnings; while (<>) { s/^>//g; }
Pretty simple, right? Well, all that does is obliterate, simply clobber the file. Any idea what's wrong with the search and replace? Any help with this regex would be greatly appreciated.

thx!

Replies are listed 'Best First'.
Re: Remove '>' from email
by quester (Vicar) on Mar 05, 2008 at 07:29 UTC
    Actually, if you add a print statement, ikegami's suggestion of perl -i will work:
    #!/usr/bin/perl use strict; use warnings; while (<>) { s/^>//g; print; }
    Otherwise your script has no output...!
Re: Remove '>' from email
by Corion (Patriarch) on Mar 05, 2008 at 06:56 UTC

    I think you will need to show us how you're running the program. My guess is that you're trying to run your program as follows:

    perl -w 672099.pl <mymail.txt >mymail.txt

    Running any program like that will always clobber the file, because that's how the shell works. It first creates all output files and then reads from all input files. But your output and input file are the same.

    To see that Perl is not the problem, run your program without output redirection.

      Solutions:
      672099.pl <mymail.txt >mymail.txt.new && mv mymail.txt.new mymail.txt
      or
      perl -i 672099.pl mymail.txt
Re: Remove '>' from email
by ikegami (Patriarch) on Mar 05, 2008 at 07:01 UTC
    By the way, the g modifier is useless (but harmless) in your code since ^ can only match at one spot.
Re: Remove '>' from email
by moritz (Cardinal) on Mar 05, 2008 at 07:31 UTC
    You forgot to print the result of the search&replace command.
Re: Remove '>' from email
by chromatic (Archbishop) on Mar 05, 2008 at 06:57 UTC
    Well, all that does is obliterate, simply clobber the file.

    How are you invoking it?

Re: Remove '>' from email
by KurtSchwind (Chaplain) on Mar 05, 2008 at 13:46 UTC
    Perl does allow you to run regex 'in place'. The below example should work.
    perl -i -ape 's/^>//g' file.txt
    --
    I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.
      perl -i -ape 's/^>//g' file.txt
      

      The '-a' is unnecessary, since there's no need to auto-split anything. Replacing it with a '-w', though, might be of benefit since mistypes happen. :)