in reply to Re: File::stats issue
in thread File::stats issue

yes, sure... here's the code i am stuck on
#!/usr/bin/perl use File::stat; use File::Basename; $srcdir = "/nfs/spptribu/"; $dstdir = "/trib/oper/temp/sql/eventual/"; $pgmFind="/trib/oper/temp/copiaFind"; open(DATOS,"$pgmFind|"); @datos = <DATOS>; close(DATOS) ; foreach $archivo (sort @datos) { #print "datos:$archivo"; ($perm,$nol,$owner,$group,$size,$mes,$dia,$hora,$filename) = split + (/\s+/, $archivo); $namefile=basename($filename,@suffixlist); $srcfile="$srcdir$namefile"; $dstfile="$dstdir$namefile"; # printf ("Archivo: %s \n\tPermisos: %s \n\tOwner: %s \n\tGroup: %s +\n\tSize: %s \n\tMes: %s Dia: %s Hora: %s\n",$filename, $perm,$owner,$group,$size,$mes,$dia,$hora); print "origen: $srcfile\n"; print "destino: $dstfile\n"; my ($src_dev,$src_ino,$src_mode,$src_nlink,$src_uid,$src_gid,$src_rdev +,$src_size,$src_atime,$src_mtime,$src_ctime,$src_blksiz e,$src_blocks)=stat($_); my ($dst_dev,$dst_ino,$dst_mode,$dst_nlink,$dst_uid,$dst_gid,$dst_rdev +,$dst_size,$dst_atime,$dst_mtime,$dst_ctime,$dst_blksiz e,$dst_blocks)=lstat($dstfile) or die "Cagaste! destino malo"; ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$bl +ksize,$blocks)=stat('/trib/oper/scripts/planesH.s.P370' ); print "$srcfile: $src_mtime $src_size\n"; print "$dstfile: $dst_mtime $dst_size\n"; exit; }
i am trying to get an output similar to an "ls -l $filename" for each of the two files of each of the files in an array of filenames...

Replies are listed 'Best First'.
Re^3: File::stats issue
by moot (Chaplain) on Mar 14, 2005 at 20:32 UTC
    Oops! Looks like you're using File::stat's stat command as if it were the perl built-in stat()! The first returns an object (or sets fields, potentially). The second returns a list of values, as you are expecting in your code.
      but on the documentation i could find on File::stat, it says that it overrides the functionality of the built in stat(), and gives just ONE example with 1 piece of code that suposedly works with the file mode.... i've not found the man/doc for File::stat very helpful, not does it give a detail of the fields it works with, or in what format are those, hence my problems...
        Yes, but 'overrides' != 'repeats'.

        From File::stat:

        This module's default exports override the core stat() and lstat() functions, replacing them with versions that return "File::stat" objects. ... You may also import all the structure fields directly into your namespace as regular variables using the :FIELDS import tag. (Note that this still overrides your stat() and lstat() functions.)
        and from stat:
        stat Returns a 13-element list giving the status info for a file, either the file opened via FILEHANDLE, or named by EXPR.
        I think the key word there is 'replace'. Nowhere in File::stat does it say that it returns a 13 element list indentical to stat.

        You'll also want to familiarise yourself with the C library stat() function, since that is from where the field names are drawn.

Re^3: File::stats issue
by Tanktalus (Canon) on Mar 14, 2005 at 20:36 UTC

    For starters, you're using $_ rather than $srcfile in your stat call. That call likely fails.

    Secondly, when you use File::stat, you are no longer using a stat or lstat that you see in perlfunc. You're using the ones found in File::stat. These return objects, not an array of items. So you'd get something like:

    my $src_stat = stat($srcname); my $dst_stat = lstat($dstfile); my $planesH_stat = stat('/trib/oper/scripts/planesH.s.P370'); printf "%s: %s %d\n", $srcfile, $src_stat->mtime(), $src_stat->size(); printf "%s: %s %d\n", $dstfile, $dst_stat->mtime(), $dst_stat->size();
      Tanktalus.... i've already tried to do something like that, but it gave me an error, complaining that the method couldn't be called on an undefined value....

        In this case, it's because the stat or lstat call failed. Check the return value:

        my $src_stat = stat($srcname) or die "Can't stat $srcname: $!"; my $dst_stat = lstat($dstfile) or die "Can't lstat $dstfile: $!"; my $planesH_stat = stat('/trib/oper/scripts/planesH.s.P370') or die "C +an't stat /trib/oper/scripts/planesH.s.P370: $!";
        Then re-run, and see what it dies of. Hopefully $! gives you enough info to keep going from here.