FAQ
Hi Ambuli,

a few comments on your code:
On Friday 20 May 2011 14:37:52 Ambuli wrote:
Here i paste a perl script to delete last Two Lines. If you want
delete more lines in a file you can specify it.

Always start with "use strict;" and "use warnings".
use File::ReadBackwards;
Include some empty lines between logical paragraphs of your code.
my $filename = 'test.txt';
This is better specified as a command line argument
my $Lines_to_truncate = 2; # Here the line to truncate is mean Remove
only Last Two Lines
The style of your variable name is very strange. It should be:

[code]
my $num_lines_to_truncate = 2;
[/code]

Also consider specifying it using Getopt::Long.
my $bw = File::ReadBackwards->new( $filename )
or die "Could not read backwards in [$filename]: $!";
The "or die" should be indented.
my $lines_from_end = 0;
until( $bw->eof or $lines_from_end == $Lines_to_truncate )
{
print "Got: ", $bw->readline;
$lines_from_end++;
}
The print here is redundant and only adds noise and clutter to the output.
truncate( $filename, $bw->tell );
It would be safer to keep track of the position, then destroy $bw, and only
then truncate the file.

Regards,

Shlomi Fish

--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Understand what Open Source is - http://shlom.in/oss-fs

Beliefs are what divide people. Doubt unites them.
-- http://en.wikiquote.org/wiki/Peter_Ustinov

Please reply to list if it's a mailing list post - http://shlom.in/reply .

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 3 | next ›
Discussion Overview
groupbeginners @
categoriesperl
postedMay 20, '11 at 11:38a
activeMay 20, '11 at 4:03p
posts3
users3
websiteperl.org

3 users in discussion

Ambuli: 1 post C.DeRykus: 1 post Shlomi Fish: 1 post

People

Translate

site design / logo © 2021 Grokbase