In article <[EMAIL PROTECTED]>,
 [EMAIL PROTECTED] writes:
>ok so $^ I is only for the diamond operator.  Understood.  So for 
>situations when not using the diamond operator, $^ I for making backup 
>copies is useless, so thr rename function will have to suffice.
>
>Is this the best way?

Usually you can use the diamond operator in any circumstances.  Just
put the filename in @ARGV and make the loop.  See the example program below.

Now, if you wonder, "But what if I want to print something to the usual
default filehandle during that loop, e.g., to track progress?" then you can
explicitly name the filehandle, usually STDOUT:

        print STDOUT "Reached line $. of input\n";

Note that <> will empty @ARGV when the loop is done.

I can't think of any case where an in-place edit is intended that the diamond
operator and $^I can't be used to accomplish it.

>[EMAIL PROTECTED] (Peter Scott)

>Take a look at what I wrote before:
>  --
>Nowhere do you use the contents of @ARGV.  In-place edit only works
>when you use the diamond operator (<>) for I/O, and you haven't.
>So, having defined $^I and set @ARGV, you then need a loop:
>
>        while (<>) {
>                # Do something with $_
>                # print something to default filehandle
>        }
>
>Whatever you print to the default filehandle inside that loop will
>go to the new file.
>  --
>
>Here's a sample program, run it and see what it does, and experiment
>with it if need be.  Then you will understand how to use $^I.
>
>#!/usr/local/bin/perl
>use strict;
>use warnings;
>
>my $file = "INSERT NAME OF FILE TO MODIFY HERE";
>print "Starting in-place edit of $file\n";
>
>($^I, @ARGV) = (".bak", $file);
>while (<>)
>{
>  s/e/x/g;
>  print;
>}
>
>print "Done in-place edit, now look at $file\n";
>
>
>Look also at "perldoc perlrun" under "-i".


-- 
Peter Scott
http://www.perldebugged.com/
*** NEW *** http://www.perlmedic.com/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to