Re: [SLUG] simple perl script problem

From: Paul M Foster (paulf@quillandmouse.com)
Date: Wed Mar 09 2005 - 17:56:01 EST


On Wed, Mar 09, 2005 at 04:03:14PM -0500, aaron steimle wrote:

> Can anyone tell me why this code will NOT work? I have look every where
> and tried every combination of syntax I could find. This code is
> basically a copy of every example I can find. I know there are other
> ways to do this but I want to know why THIS isn't working.
>
>
>
> ==================================
>
> #!/usr/bin/perl
>
> opendir DIR, "/var/errors/";
>
> while ( $filename = readdir(DIR) )
> {
> @fileinfo=stat($filename);
> $sizestat = $fileinfo[7];
> print "$filename - $sizestat\n";
> }
> closedir DIR;

Well, of course! Your parentheses are in the wrong place! They have to
be like this to work right:

while ( $filename = readdir(DIR) ) {
   @fileinfo=stat($filename);
    $sizestat = $fileinfo[7];
    print "$filename - $sizestat\n";
}
closedir DIR;

Just kidding! ;-}

The real problem is that the readdir is returning a relative file name
(junk.txt as opposed to /var/errors/junk.txt). So stat is trying to pick
up data about a file that probably doesn't exist in your current
directory (where it expects the relative filename to be).

Do it this way instead:

@fileinfo = stat("/var/errors/" . $filename);

I tried that, and it works.

Paul
-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS). Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 20:26:05 EDT