Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

file size test on 5.005_3?

by djbiv (Scribe)
on Oct 15, 2003 at 18:56 UTC ( [id://299495]=perlquestion: print w/replies, xml ) Need Help??

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

I'll start of with the code, then ask the question....
#!/usr/bin/perl -w use strict; my $dir = "."; # relative path within chroot opendir(DIR, $dir) or die "can't open $dir for read: $!\n"; my @file = readdir DIR; closedir(DIR) or die "error closing $dir: $!\n"; foreach my $file (@file) { my $fsize = (-s $file); # get the file size my $isdir = (-d $file); # get the file size if ($fsize > 1 || $isdir =~ 1) { print ":$file:$fsize:$isdir:\n"; ...do more junk.... } else { print ":$file:$fsize:$isdir:\n"; ...do more junk.... } }
now I would expect this to return the filename followed by the file size and a value of 1 if it is a directory. which works great on 5.8.0 Active State... However when I run the same code on AIX running 5.005_3 I get "strange" results. the filename, size, and directory indicator are returned for '.' and '..' but every file or directory after that a size and directory indicator are not returned? I check 'perldoc -f -s' on the AIX box and it appears that the file test operators should work the same on 5.005_3 as they do on my windows box....??? can anybody shed some light on this for me?

Replies are listed 'Best First'.
Re: file size test on 5.005_3?
by davido (Cardinal) on Oct 15, 2003 at 19:18 UTC
    This probably isn't contributing to your problem, but one thing that jumps out at me as looking a little funny is this:

    $isdir =~ 1

    Essentially, that will evaluate to true if $isdir contains 1 anywhere in it. So 1, 121, 212, 00111 would all evaluate to true in that context. I just never see the binding operator (=~) being used to bind a scalar to a literal digit instead of a regexp, but it works the same as /1/. The only reason I bring it up is that by dropping the m//, it leads me to wonder if you expected the regexp behavior that you're getting.

    Maybe you meant to use '==' or 'eq', or '=~ /1/' (for clarity).


    Dave


    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein
Re: file size test on 5.005_3?
by Thelonius (Priest) on Oct 15, 2003 at 19:17 UTC
    You need to include the directory name with the file name, e.g.
    my $fullfilename = "$dir/$file"; my $fsize = (-s $fullfilename); # get the file size my $isdir = (-d $fullfilename); # get the file size
    Updated: Oh, $dir = ".". In the words of Emily Litella, "Never mind!"
Re: file size test on 5.005_3?
by Abigail-II (Bishop) on Oct 15, 2003 at 20:13 UTC
    Well, it very well may be that you have stumbled on a bug in Perl. However, 5.005_03 was released more than 4 years ago, and we have had 2 major releases, 2 minor releases, and a whole battery of development releases since.

    Please confirm whether you have the same problems with 5.8.1. If not, it was a bug in Perl, and it was fixed.

    Abigail

      I don't have this problem in 5.8.0, and unfortantly cannot upgrade the AIX box at this time... Are there any other ways to retreive the file size other then "-s" and stat ???
      UPDATE: "stat" actually returns the same results as -s on 5.005_3
        It's a strange environment you work in, if you can't replace buggy software where to bug make your programs fail. I wonder, if your monitor breaks, it won't be replaced either?

        Anyway, if you can't use -s or stat, you could always open the file, seek to the end, and use tell(). Of course, that might be broken too.

        Abigail

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://299495]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-18 19:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found