Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re^5: Empty File Contents before writing

by RichardK (Parson)
on Feb 19, 2013 at 11:41 UTC ( #1019529=note: print w/replies, xml ) Need Help??

in reply to Re^4: Empty File Contents before writing
in thread Empty File Contnts before writing

The help for truncate says

The position in the file of FILEHANDLE is left unchanged. You may want + to call seek before writing to the file.

So, any ideas why your code works without needing a seek 0?

Replies are listed 'Best First'.
Re^6: Empty File Contents before writing
by johngg (Abbot) on Feb 19, 2013 at 14:37 UTC

    You are right. I hadn't read the documentation carefully enough and hadn't spotted that. Running hexdump -C shows that a seek is indeed necessary.

    $ perl -Mstrict -Mwarnings -e ' > open my $fh, q{>}, q{rubbish} or die $!; > print $fh qq{A longish line of text\n}; > truncate $fh, 0 or die $!; > print $fh qq{Short\n}; > close $fh or die $!;' $ cat rubbish Short $ hexdump -C rubbish 00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |......... +.......| 00000010 00 00 00 00 00 00 00 53 68 6f 72 74 0a |.......Sh +ort.| 0000001d $

    Thank you for the correction.



Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2016-10-24 17:26 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (309 votes). Check out past polls.