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

truncate or trunc

by jscjso (Initiate)
on Apr 25, 2008 at 16:21 UTC ( #682864=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I have problem to use truncate or trunc on a filehandle. Both of these error my Perl script out. Do I need to load a module or activate it at the top of my script?

Replies are listed 'Best First'.
Re: truncate or trunc
by Fletch (Bishop) on Apr 25, 2008 at 16:30 UTC

    No, truncate is a builtin part of the language. Perhaps if you provided sample code which exhibits the problem or an actual error message someone could possibly hope to help? See also How (Not) To Ask A Question.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: truncate or trunc
by citromatik (Curate) on Apr 25, 2008 at 16:34 UTC

    What errors do you have?

    This works for me:

    open my $fh, "+<", $filename or die $!; truncate $fh,$pos or die $!;

    or directly

    truncate $filename,$pos or die $!;

    Hope this helps


Re: truncate or trunc
by apl (Monsignor) on Apr 25, 2008 at 16:33 UTC
    To expand on what Fletch said, truncate requires two parameters, the second being the length of the file you wish to have left. Did you specify anything other than the filehandle?
Re: truncate or trunc
by mr_mischief (Monsignor) on Apr 25, 2008 at 16:37 UTC
    Does this give the error:
    open my $file, '+>', 'testfile' or die "Can't write testfile: $!\n"; print $file 'test!'; seek $file, 0, 0; print <$file> . "\n"; seek $file, 0, 0; truncate $file, 0 or die "Can't truncate testfile: $!\n"; close $file or die "Can't save testfile: $!\n";

    If so, what's the error? truncate says that if truncate isn't implemented it's a fatal error, but I'm not sure what OSes that would include.

    BTW, it's usually a good idea to show us what you've tried so we can work from that.

Re: truncate or trunc
by jscjso (Initiate) on Apr 25, 2008 at 16:59 UTC
    Hi, Thanks for the quick replies. Here is my code: while (<DATA>){ unless (/$data_line_old/i){ my $read_position = tell DATA; seek DATA, $write_position, 0; print DATA $_; $write_position = tell DATA; seek DATA, $read_position, 0; } } truncate DATA, $write_position; close DATA; I tried to remove old lines from a text file. The tell and seek monitor the positions of the existing file and temporary file that it is creating. At the end of the creation. I try to use the truncate or trunc to cut the finished file to the right size(length). After the confidence you gave about the truncate, I tried again a few more times and discovered what went work. I initial used trunc and it errors me out as: Can't locate object method "trunc" via package "IO::Handle" at line 514, <DATA> line 10. Since this error, the original file got messed up. Then the truncate went wild after a few runs. The text file became super long with wrong length. I have it working now. Thanks, Jason
      Please bracket your code with <code> and </code>. Thanks.

      I assume you opened a file with the filehandle DATA? Please change that to something else. DATA is reserved for a block of sample data within your program.

      Then you might consider testing your error returns...

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2023-09-28 23:51 GMT
Find Nodes?
    Voting Booth?

    No recent polls found