in reply to Subroutine speed

Sorry Monks ,

I am totally new to this kind of a technical group and am getting aware of how to list out the problems.

Thanks to all those who have identified my mistakes in defining the problem.

the code is as follows , I hope now people can easily test it out and bring the best to me.

</p> #!/usr/bin/perl -w use Strict; use File::Stat; use Digest::MD5; print "Enter the Path where all the data files are available:\t"; $sourcedir= <STDIN>; chomp($sourcedir); open(LOG ,">>D:\\prad\\log1.csv"); #$workbook = Spreadsheet::WriteExcel->LOG('D:\prad\log1.csv'); print LOG "Report Ver - 0.1\n"; print LOG"_______________________________\n"; print LOG "\nFilename,Md5sum(oneof),Size,Md5Sum(oneofcmd_cln),Size,Md5 +Sum(oneofgsd_cln),Size"; print LOG"\n___________________________________________\n"; list_recursively("$sourcedir\\oneof"); exit; ###################################################################### +########## # Subroutine ###################################################################### +########## # list_recursively # # list the contents of a directory, # recursively listing the contents of any subdirectories # sub list_recursively { my($directory) = @_; my @files = ( ); # Open the directory unless(opendir(DIRECTORY, $directory)) { print "Cannot open directory $directory!\n"; exit; } # Read the directory, ignoring special entries "." and ".." @files = grep (!/^\.\.?$/, readdir(DIRECTORY)); closedir(DIRECTORY); # If file, print its name # If directory, recursively print its contents # Notice that we need to prepend the directory name! foreach my $file (@files) { # If the directory entry is a regular file if (-f "$directory/$file") { $filepath="$directory"; $filepath2=$filepath; $filepath2=~s/oneof/oneofcmd_cln/; $filepath3=$filepath; $filepath3=~s/oneof/oneofgsd_cln/; #Finding the size of the file #digesting open(FILE ,"<$filepath\\$file"); binmode (FILE); $file_size="$filepath\\$file"; @st = stat($file_size) or die "No $file: $!"; $digest = Digest::MD5->new->addfile(*FILE)->hexdigest; #print file name in Log file print LOG "\n$filepath\\$file"; printf LOG",%s,%s",$digest,$st[7]; close(FILE); if(open(FILE, "<$filepath2\\$file")) { #print LOG "in MD2"; binmode (FILE); $file_size="$filepath2\\$file"; @st = stat($file_size) or die "No $file: $!"; $digest = Digest::MD5->new->addfile(*FILE)->hexdigest; printf LOG",%s,%s",$digest,$st[7]; close(FILE); } else{ print LOG",Null,";} if(open(FILE, "<$filepath3\\$file")) { #print LOG "in MD3"; binmode (FILE); $file_size="$filepath3\\$file"; @st = stat($file_size) or die "No $file: $!"; $digest = Digest::MD5->new->addfile(*FILE)->hexdigest; #print file name in Log file #print LOG "$filepath3\\$file"; printf LOG",%s,%s",$digest,$st[7]; #here i need to include a formula into the csv file at the #end of eac +h line. close(FILE); } else{ print LOG",Null";} # If the directory entry is a subdirectory }elsif( -d "$directory/$file") { # Here is the recursive call to this subroutine print LOG"\nFolder - $directory\\$file \n"; print LOG"----------------------------------\n"; list_recursively("$directory\\$file"); } } } ###################################################################### +##########

Any improvements in the code would be a great addition. Sorry , Thanks and Regards

Pradeep.S

Replies are listed 'Best First'.
Re^2: Subroutine speed
by blazar (Canon) on Jan 21, 2005 at 10:05 UTC
    the code is as follows , I hope now people can easily test it out and bring the best to me.
    #!/usr/bin/perl -w
    It's better to
    use warnings;
    nowadays.
    use Strict; use File::Stat; use Digest::MD5; print "Enter the Path where all the data files are available:\t";
    Are you sure you want "\t" rather than "\n"?
    $sourcedir= <STDIN>; chomp($sourcedir);
    Since you're under strict this won't even compile. It is a good thing to post real code.

    Update: Case matters! (Hadn't noticed your mistake in the first place.) Please (do a favour to yourself and) reread your program taking this into account.

    open(LOG ,">>D:\\prad\\log1.csv");
    It's better to
    • use lexical FHs nowadays,
    • use the three-args form of open(). (Opinions tend to vary on this, but as a general rule, I'd still recommend it.)
    • use simple quotes (in this case) and use / as a directory separator even under Windows,
    • always check open()s for success. A minimal
      open my $fh, '>>', 'path/to/file' or die $!;
      can suffice.
    I'm not reading the rest of your script. You should consider preparing a minimal, working, test program still exhibiting the problem you're concerned about and submit that. Chances are that in the process of doing so you will find the answer yourself...

      Just out of curiosity, why do you consider use warnings; superior to perl -w?

      -- vek --
        perldoc warnings
        perldoc perllexwarn
        perldoc perlrun
        
        Short answer: more flexibility.