-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Jeff Thies wrote:
> I have a file that I need to make some substitutions in:
>
> open FH, "my_file" or die "Can't open... $!";
>
> @file=<FH>;
>
> Can I read the file handle so it is a scalar and I can just do this?:
>
> $file=~s/replace_me/with_this/gis; # small files
>
> and then print back to a filehandle.
>
> Or should I do something like this:
>
> my @new_file=();
> while(<FH>){
> $_=~s/replace_me/with_this/gi;
> push @new_file, $_;
> }
>
> and then print @new_file to a filehandle? Or would that be a reference
> to @new_file?
You can let perl worry about opening the files, writing back to them, and even looping over the
lines, through command-line switches. This program does what you're doing above:
- -- cut here --
#!/usr/bin/perl -pi
s/replace_me/with_this/gi;
- -- cut here --
That's it ! For more info on the -p and the -i switches (and many other interesting ones), see
perldoc perlrun
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla -
http://enigmail.mozdev.org
iD8DBQE/DkOteS99pGMif6wRAvrYAKD/oylKq/WL6pLBixWlA9VCng542QCfY/dt
BbWHBtt+1GK7PCGVriMaMuA=
=6jHE
-----END PGP SIGNATURE-----