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

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 maintain.pl 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)
http://perldoc.perl.org/perlport.html#truncate
What are i am doing wrong?

Thanks,
David

Comment on problems with truncate function
Download Code
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

    Cheers!
    Tobias
      Hi,
      My file has content.
      BR,
      David
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: $!";
        to
        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

        Thanks,
        David
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 ...

    or

    truncate $fh, 0 or die ...

    Your code is doing:-

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

    which is not what you want!

    Cheers,

    JohnGG

      This post made me perl wiser :-).
      The forum "Seekers of perl wisdom" is really appropriate.
      Thank you all,
      David

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-04-23 19:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (554 votes), past polls