Click here to get back home

Re: opening file for appending doesn't work for me

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Re: opening file for appending doesn't work for me Tad J McClellan 03-18-2008
Posted by Tad J McClellan on March 18, 2008, 9:45 pm
Please log in for more thread options


> my $key_file="~/.ssh/authorized_keys"; #path the the authorized-keys file


tilde means "home directory" when programming in the shell.

tilde means "tilde character" in a Perl string...


> open(INFO, $file); #opens file systemstats


You should always, yes *always*, check the return value from open():

open(INFO, $file) or die "could not open '$file' $!";


> @lines = <INFO>; #assigns lines to array
> foreach $line (@lines){ #go through each line in file


No point in reading the whole thing into memory if you are only
going to process one line at a time anyway.

while ( my $line = <INFO> ) {


> $num++;


Now you can use the $. variable instead of maintaining the count yourself too.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"

Similar ThreadsPosted
Re: opening file for appending doesn't work for me March 18, 2008, 5:09 pm
Relative path wont work when opening a file for writing April 24, 2007, 12:37 pm
Appending to the middle of a file June 1, 2005, 11:29 am
better idiom for appending to list from file May 30, 2006, 3:22 pm
Serious Problem while appending an Excel file July 24, 2007, 10:56 am
Opening a file twice and having an if loop June 8, 2007, 10:23 pm
Error received opening file November 9, 2004, 5:17 pm
opening a file to read and write February 10, 2005, 7:39 am
replacing text at the beginning of a file in perl and appending to that output July 20, 2005, 8:33 am
Opening a text file and editing its contents January 14, 2005, 12:28 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap