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

Hi I am trying to make this into a script so that i can create a batch file to run it. perl -pi -e 's/\/Im0\ Do//g' X:\path\to\files\*.pdf It actually removes the watermark from a pdf file.I am just trying to wonder how to do an inplace edit and how to specify the substitution. Thanks for your help

Sandhya

Update: I tried editing the responses to see if that would work.
#!/usr/bin/perl use warnings; use strict; my @files = <H:\*.pdf>; @ARGV = @files; $^I = ''; while ( <> ) { s/\/Im0 Do//g; print; }
It says cannot do inplace edit without backup at line 9

Update

It compiles and runs without gitch, but the pdf pages are blank and the permissions have changed to give write privilege to everyone i think.

UPDATE, I found this.http://aspn.activestate.com/ASPN/Mail/Message/perl-win32-users/3562086..would someone like to take a shot at figuring out whats wrong?

UPDATE

FINALLY GOT IT TO WORK!!!!
#!/usr/bin/perl BEGIN {(*STDERR = *STDOUT) || die;} use diagnostics; use warnings; use strict; $| = 1; my @files = <H:\*.pdf>; @ARGV = @files; $^I = '~'; while (<>) { binmode ARGVOUT if $. == 1; s/\/Im0 Do//g; print; }

Replies are listed 'Best First'.
Re: in place edit - how to do
by jwkrahn (Abbot) on Jan 14, 2009 at 21:04 UTC

    You could use B::Deparse to translate that into a Perl program:

    $ perl -MO=Deparse -pi -e 's/\/Im0\ Do//g' BEGIN { $^I = ""; } LINE: while (defined($_ = <ARGV>)) { s[/Im0\ Do][]g; } continue { print $_; } -e syntax OK

    Which could also be written as:

    #!/usr/bin/perl use warnings; use strict; $^I = ''; while ( <> ) { s/\/Im0 Do//g; print; }
Re: in place edit - how to do
by ikegami (Patriarch) on Jan 14, 2009 at 21:30 UTC

    It says cannot do inplace edit without backup at line 9

    In-place editing currently uses a system feature that isn't available in Windows. But in-place editing with backup doesn't use that feature, so it works on Windows. Change

    $^I = '';

    to

    $^I = '.bak';

    A proposal for future version of Perls might eliminate this issue.

Re: in place edit - how to do
by FunkyMonk (Bishop) on Jan 14, 2009 at 22:52 UTC
    It says cannot do inplace edit without backup at line 9
    If you get an error message you don't understand, use diagnostics can offer extra explanation, or you can just look the error up in perldiag

    In this case, you would have read:

    Can't do inplace edit without backup (F) You're on a system such as MS-DOS that gets confused if you try reading from a deleted (but still opened) file. You have to say "-i.bak", or some such.
      The diagnostic message is wrong in this case. Windows is quite able to read from deleted (but still opened) files. It just can't can't create a new file with the same name until all handles to it are closed. The solution is appropriate, though.
        Patches welcome :-)
Re: in place edit - how to do
by zentara (Cardinal) on Jan 14, 2009 at 21:04 UTC
    Untested :-)
    #!/usr/bin/perl use warnings; use strict; $^I = ""; # make sure it prints back out my @files = <X:\path\to\files\*.pdf>; @ARGV = @files; while(<>){ s/\/Im0\ Do//g; print; # yikes, without the print you will wipe out the file }

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: in place edit - how to do
by moritz (Cardinal) on Jan 14, 2009 at 21:07 UTC
    If it works as is, why can't you put it in a batch file as is?
      I am unable to use it as such because i also need to perform other actions to the file as and when the watermark is removed. Thanks