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

Greets, Having a little trouble....I'm creating a directory structure tree that is output to html. When I use -w on the she-bang line I continually get the message...."Use of uninitialized value in print.....use of uninitialized value in numeric le (<=) at line....." I'm essentially running a filetest to check the modification age of the document stored in the search dir, yet all of my output states the files are "brandnew". This is very bewildering to me. Can anyone take a look at this code and help me out? (I won't bother including the myhtml.pm code used, since that functions properly)
#!/usr/bin/perl -w use myhtml; use strict; print"Please enter the directory to be searched. "; chomp(my $dirname = <STDIN>); until(-d $dirname){ print"The directory entered is not valid.\nPlease enter a director +y to be searched. "; chomp($dirname = <STDIN>); } unless($dirname =~ /\\$/){ $dirname .= "\\"; } open OUT, ">dir_stuff.html" || die "Could not open file: !, $!"; my $beginning = &myhtml::start_doc($dirname); print OUT <<MYHTML; $beginning MYHTML print OUT "$dirname<br>\n"; &search($dirname, 0); my $ending = &myhtml::end_doc(); print OUT <<MYHTML; $ending MYHTML sub search(){ my($dir, $offset) = @_; my $fullname; opendir DIR, $dir; foreach my $file(readdir DIR){ next if ($file =~ /^\./); if($file =~ /\.pl$/){ $fullname = $dir . $file . "\\"; &printFile($file, $offset, $fullname, 0); } if($file =~ /\.html$/){ $fullname = $dir . $file. "\\"; &printFile($file, $offset, $fullname, 1); } if(-d $dir.$file){ &printFile($file, $offset, $fullname, 2); $fullname = $dir . $file. "\\"; &search($fullname, $offset + 1); } } } sub printFile(){ my @file_type = qw(perl html dir); my @file_color = qw(orange red green); my($file, $offset, $filename, $type) = @_; my $spacer; my $age; my $days_old = (-M $filename.$file); if($offset > 0){ $spacer ="|" . "&nbsp;" x (4 * $offset); } if ( $days_old <= 7 ){ $age = "<b><i>brand</i>new!</b>"; }elsif ( $days_old < 30 ){ $age = "<b>new!</b>"; }else{ $age = "";} print OUT $days_old; print OUT "$spacer|----<font color= $file_color[$type]>$file_type[ +$type]: $file</font> $age<br>\n"; }
The html file created looks correct except the modification stamp isn't accurate for the files. Thanks in advance....

Replies are listed 'Best First'.
Re: File Test help with -M (modification age)
by CountZero (Bishop) on Dec 12, 2002 at 07:22 UTC

    Are you sure my $days_old = (-M $filename.$file); is correct?

    It looks as if you are concatenating the full file name (path + filename) with the filename itself.

    When I run your program $days_old never gets any value.

    When I use my $days_old = -M $filename; it works.

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: File Test help with -M (modification age)
by particle (Vicar) on Dec 12, 2002 at 02:31 UTC
    $fullname = $dir . $file . "\\";

    are you sure you don't mean

    $fullname = $dir . '\\' . $file;

    ~Particle *accelerates*

      I've already added a \ after dir using this statement if one does not exist....
      unless($dirname =~ /\\$/){ $dirname .= "\\"; }
      so by doing
      $fullname = $dir.$file."\\";
      I just continue to build the path to the file with the trailing '\'