RE: File Handling

From: Philipp Traeder (Traeder_at_ImmobilienScout24.de)
Date: 04/05/04


To: "'senthil prabu'" <senthilprabu333@yahoo.co.in>, beginners@perl.org
Date: Mon, 5 Apr 2004 12:00:49 +0200 

Hi Prabu,

> I have wrote a script to search for a pattern and replace
> it in all files of a directory,that i specified at
> commandline.I want another one thing is to be done in the
> script.That's,it should search only for the type of files I
> specified at commandline.That is,it should get the extension
> of file name and search only those files in the directory.

> I have made a attempt in this script,but it goes on asking
> extension for all the files in directory.
> Plz help me in getting the things right in the following script.
> Tell the changes to be made in this.
>
> #! /usr/bin/perl
>
> print "Enter a path name: ";
> my $path=<STDIN>;
> chomp($path);
> opendir THISDIR, "$path" or die "serious dainbramage: $!";
> my @allfiles = readdir THISDIR;
>
> # get all files
> foreach $file (@allfiles){
> $filetoopen = $path ."/" .$file;
>

The following lines are the problem:

> # filter to check the type of file
> print "Enter the type of extension:";
> my $ext=<STDIN>;
> chomp($ext);

You're asking for the extension inside the foreach-loop, therefore your
application
asks you for the extension everytime it processes a file. Simply move those
lines up
(in front of the "get all files" comment), and everything should work. :-)

> ($str1,$str2) = split(/./, $filetoopen);
> if($str2 eq $ext)
> {
> print $str2;
> $filetoopen1 = join(".",$str1,$str2);
> print $filetoopen1;
> open(IN, "<$filetoopen1") || die "cannot open file\n";
> open(OUT, ">$test") || die "cannot open file\n";
> while (<IN>){
> if (/$com/){
> s/$com/td>\n</g">\\script>/g;
> }
> if (/$img/){
> s/$img/\n<script>\n<img/g;
> }
> if (/$pattern/){
> s/$pattern/$own/g;
> # print $_;
> }
> if (/img/){
> s/$img/document.write("<img/g;
> }
> if (/$com/){
> s/$com/td>");/g;
> }
> print OUT $_;
> }
> close (OUT);
> close (IN);
> rename("$test","$filetoopen1");
> }
> }
> __END__
>
>

The problem you're trying to solve - replacing one pattern with another in
multiple
files - is actually a very common one for perl scripts. Thus there´s some
built-in
perl magic for handling scenarios like this:

A) working on multiple files line per line

Take the following code and save it in a perl script called
multiplefiles.pl:

##### START multiplefiles.pl #####
#! /usr/bin/perl -w

use strict;

while (<>) {
    print "processing line ($_) in file $ARGV\n";
}
##### END multiplefiles.pl #####

Now call it on command line like this:
        multiplefiles.pl myfile1.txt myfile2.txt

As you hopefully see, this code opens every file and processes it line by
line...

B) Globbing

Adding one line, perl lets you specify wildcards on the command line:

##### STARTUP multiplefiles2.pl #####
#! /usr/bin/perl -w

use strict;

@ARGV = glob(join(' ', @ARGV));

while (<>) {
    print "processing line ($_) in file $ARGV\n";
}

##### END multiplefiles2.pl #####

Now you can call this script like this:
      multiplefiles2.pl myfile*.pl some*stuff.txt file2

...and Perl will do the rest for you.

By the way: I'd recommend to change the first lines of your script to:

#! /usr/bin/perl -w

use strict;

This enables some more warnings and checks - very helpful to avoid
mistakes...
Also, I'd indent blocks for better readability - something like:

if ($str2 eq $str1) {
    foreach (@values) {
        print "doing something.\n";
    }
}

HTH,

Philipp



Relevant Pages

  • Re: New Application Extension reports 404
    ... You actually want .RUN to be handled by Perl, ... please add a Web Service Extension ... /command1.run will execute Perl script1.pl ... but IIS4 had the "RUN" extension mapped to execute a predefined perl script. ...
    (microsoft.public.inetserver.iis)
  • Re: Perl from Terminal in OSX
    ... The -e switch tells perl to evaluate the text given to it as the script, ... school Perl extension for libraries of code, ... would suggest that the extension is incorrect. ...
    (perl.beginners)
  • Re: abc (musical notation) package by Enrico Gregorio
    ... On windows there is Active Perl distribution which works fine. ... extension; ... script with the extension. ...
    (comp.text.tex)
  • Re: Extending file associations in DOS
    ... Why not make your script a batch file? ... goto end ... The PERL interpreter looks for the line starting with a shebang and ... I give these scripts the extension .PL. ...
    (comp.os.msdos.misc)
  • Re: an original perldoc viewer
    ... Since you have invoked your script via the perl ... the -T option on the shebang line but also specify it on the command line ...
    (comp.lang.perl.misc)