Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

checking if file is empty or not

by damian (Beadle)
on Aug 07, 2000 at 10:50 UTC ( [id://26509]=perlquestion: print w/replies, xml ) Need Help??

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

hi fella monks, how will i know with perl if a file is empty or not. newbie

Replies are listed 'Best First'.
Re: checking if file is empty or not
by tilly (Archbishop) on Aug 07, 2000 at 11:04 UTC
    if (-s $filename) { # The file is not empty }
    For more handy file-tests type perldoc perlfunc and search for "test". (For most people that means type "/test" and hit return.)
Re: checking if file is empty or not
by ColtsFoot (Chaplain) on Aug 07, 2000 at 11:16 UTC
    You can use stat 'Filemane' this returns a 13 element list giving the statistics
    for the file "Filename" so
    @res = stat "file_to_stat"; print qq($res[7]\n);
    will return the size of the file in bytes, the other elements of the list are as follows
    dev - Device number ino - Inode number mode - File node (type and permissions) nlink - No of hard links uid - File owner gid - Group Id rdev - Device identifier size - size in bytes atime - access time mtime - modified time ctime - Inode change time blksize - Prefered block size blocks - actual number of blocks allocated
    Note: Not all fields are supported bt all OSs.
    Hope this is of help
Re: checking if file is empty or not
by lolindrath (Scribe) on Aug 07, 2000 at 16:03 UTC
    hmm, this works but is probably inefficient.
    open FILE, "Filename" || die; @file = <FILE>; close FILE; $file = join( "", @file); if ( $file ne "" ) { #there's something there! }

    Just thought I'd pose another solution

    --=Lolindrath=--
      There are file operators out there that will test the size and/or if it's an empty file already created, and there is no need to create a new way to do it.
      It's good to know that you are thinking, and I am happy that you are willing to participate, but there is a reason why laziness is a virtue. :)

      Cheers!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-26 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found