Help gettgin this script to work on linux

This script used to work on windows but is erroring our at line 25 when run under linux. The specific errir that i get is "Can't call method "mtime" on an undefined value at ./make_event_log_index.pl line 25."

Any help would be appreciated

....................................................................................................

#!/usr/bin/perl -w

use File::Find;
use File::stat;

for ($count = 1; $count <= 23; $count++){
  $#file = -1; # Zero-out array
  $dir = "/home/bed_mfg1/event_logs";
  find(\&Wanted, $dir);
  @file=reverse(sort(@file));
  open (FILE, "> /tmp/event_logs_listing.tmp") or die "Could not open file: $!";
  print FILE "@file";
  close FILE;
  rename "/batch/event_logs_listing.tmp", "/batch/event_logs_listing.txt";
  $#file = -1; # Zero-out array after run (so memory's freed for sleep cycle)
  sleep 3300;  # Roughly the amount of time, when added to execution time, to equal an hr
}


sub Wanted
  {
    my $name = $File::Find::name;
    $name =~ s/\//\\/g;
    $size = -s "$name";
    $date = stat($name)-> mtime;
    my ($sec, $min, $hour, $day, $month, $year) = (localtime($date))[0,1,2,3,4,5,6];
    $year+=1900;
    $month++;  # I have _NO_ idea why $month is off by one.  None.
    if ($min < 10){$min = "0".$min;}
    if ($hour < 10){$hour = "0".$hour;}
    if ($month < 10){$month = "0".$month;}
    if ($day < 10){$day = "0".$day;}

    $name =~ s/\\/\\\\/;
    $file[$#file+1]  = "$year-$month-$day".'_'."$hour:$min".",".$name.",".$size."\n";
  }

Because 2 lines above that you're replacing all forward slashes (/) with backslashes (\), which are an escape character on all Unix platforms, not path separators.

And about your comment on why $month is "off by one": by convenience since I don't know when, as you can use the value directly as an array index for month names (as outlined in the documentation for localtime), and you can use it in mod 12 calculations without having to increment/decrement all the time.

The last line, by the way, could be written much more elegantly by using push and sprintf :wink:

Remove that $name =~ s/\//\\/g; regex, UNIX doesn't need it. You're replacing all the forward slashes with back slashes, causing invalid\paths\like\this to be fed into stat().

[edit] Curses, foiled again.

BTW, if you're developing for multiple platforms, you might want to take a look at File::Util (sadly, not part of the core modules), which will do all this mapping for you automagically.

Also: There can be only one! [cue Thunder sound]

Your code is unsecure against symbolic links that point to non-existing files.
Try this modified subroutine:

sub Wanted
{
my $name = $File::Find::name;

unless($st = stat($name)) {
  print STDERR "could not stat file: $name\n";
  return;
}
$size = $st->size;
$date = $st->mtime;
my ($sec, $min, $hour, $day, $month, $year) = (localtime($date))[0,1,2,3,4,5,6];
$year+=1900;
$month++; # I have _NO_ idea why $month is off by one. None.
if ($min < 10){$min = "0".$min;}
if ($hour < 10){$hour = "0".$hour;}
if ($month < 10){$month = "0".$month;}
if ($day < 10){$day = "0".$day;}

$file[$#file+1] = "$year-$month-$day".'_'."$hour:$min".",".$name.",".$size."\n";
}