Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

problems with truncate function

by david2008 (Scribe)
on Jun 26, 2013 at 07:37 UTC ( #1040714=perlquestion: print w/replies, xml ) Need Help??
david2008 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have the following code
my $fh = FileHandle->new(">>$log_file") || die "could not open $log_fi +le: $!"; truncate $fh,0 || die "could not truncate $log_file: $!"; $fh->close() || die "could not close $log_file: $!";
When running the code i get the error
could not truncate ....: Inappropriate ioctl for device at line 39.

I tried to find the solution with google.
The only restriction i found is that the file has to be opened in append mode (which is our case)
What are i am doing wrong?


Replies are listed 'Best First'.
Re: problems with truncate function
by johngg (Abbot) on Jun 26, 2013 at 09:29 UTC

    What Anonymonk is saying is the || has a higher precedence than or so either write:-

    truncate( $fh, 0 ) || die ...


    truncate $fh, 0 or die ...

    Your code is doing:-

    truncate $fh, ( 0 || die ... )

    which is not what you want!



      This post made me perl wiser :-).
      The forum "Seekers of perl wisdom" is really appropriate.
      Thank you all,
Re: problems with truncate function (perloperator precedence or ||)
by Anonymous Monk on Jun 26, 2013 at 08:04 UTC
        Hi, You are correct :-)
        I ran the perl with the B::Deparse module
        Perl parses the line
        truncate $fh,0 || die "could not truncate $dcs_log_file: $!";
        truncate $fh, die("could not truncate ${log_file}: $!");
        Now i also understand the difference between || and or :-)

        In || it makes 0|| before truncate.
        with or it would make the truncate before

Re: problems with truncate function
by tobias_hofer (Friar) on Jun 26, 2013 at 07:59 UTC
    I gave a try with a simple file and it works fine.
    You truncate to 0, means your file will be empty afterwards.
    if size, 0 in your case, is greater than the files length then the behavior is undefined.
    Means you got to have some content in your file. Do you have content in your file?
    See: tuncate

      My file has content.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1040714]
Approved by Corion
[erix]: D.: yeah, I am in the low countries. To be precise, I am 0.5m above sea level
[erix]: going for a run in the dunes - unless the storm (well, Beaufort 7) gets fiercer
Discipulus had stew with tomato sauce, onions, carrots and potatoes + bottle of red wine and still warm bred.. slept as dynosaur

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (14)
As of 2017-11-22 09:14 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (316 votes). Check out past polls.