Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Applying multiple file modes

by afoken (Chancellor)
on May 25, 2015 at 06:50 UTC ( [id://1127652]=note: print w/replies, xml ) Need Help??


in reply to Applying multiple file modes

open( my $testfile, "+>> $file" ) $!\n"; if ( -z "$testfile" ) ---> checking for empty file

This does not even compile. Post the actual code.

Stringifying the file handle in $testfile will give you something like GLOB(0xb03cb8), NOT the filename. This test will always fail.

To test if the file is empty, you do not need the -z test at all. You open the file for appending, so the file position is set to the end of the file. This is at position 0 only if the file is empty. tell will tell you the file position, or -1 on error. So, to repair that test:

open my $testfile,'+>>',$file or die "Can't open '$file': $!"; my $pos=tell $testfile; ($pos!=-1) or die "tell failed for '$file': $!"; if ($pos==0) { # file is empty } else { # file has some content }

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-20 04:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found