Sponsored Content
Top Forums Shell Programming and Scripting Perl : How is file handling working here?? Post 302831881 by balajesuri on Friday 12th of July 2013 06:19:17 AM
Old 07-12-2013
Everytime you do read from a filehandle and store it in a scalar variable, then one line of input is read. (By one line, I mean the data until the next record separator).

Here is how it works internally:
When you open a filehandle, lets say, there's a mark that is at the beginning of file. The first time $rec = scalar <$INF> is encountered, the first line from file is read and referred by $rec. Now, the mark that we spoke of earlier, is at the character just after newline character (assuming newline as the default record separator). Now again when $rec = scalar <$INF> is encountered, one more line of data is read.

In the example code you provided in your post, a wrapper routine "readfile" reads one line of data and prints it.

The usual way of reading a file is to use a loop. Then again, that depends on what you really want to do:

Code:
open FH, "< /path/to/file";
while ($rec = <FH>) {
    print $rec;
}
close FH;

 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

file handling problem in perl......

Hi, I am opening a file......then i am wrting some data into it......and i am reopening the file again but ......i get a error cannot open file....... $::file= "\adder\testfile.txt" open(TEST1,$::file); some write operation close(TEST1) open(TEST1,$::file) 'I GET A ERROR CAN OPEN... (2 Replies)
Discussion started by: vivekshankar
2 Replies

2. Shell Programming and Scripting

perl redirect output to file ..not working

here is simple perl script i wanted for my net connection ... just to check if default gateway is pingable or not if not write in log file but problem is that i can not write in file i can print on STDOUT but not in file ...why so ?? same thing was there when i was tying to write on sockets... (7 Replies)
Discussion started by: zedex
7 Replies

3. Shell Programming and Scripting

Perl revers File handling

Hi Experts, I have a big text file, so I want read it at eof to upper bound !. after I use a fseek to go SEEK_END, is it possible to step up upperbound? Best Regards. Note that I'm used perl script. (2 Replies)
Discussion started by: Zaxon
2 Replies

4. Programming

XML Handling in Perl

Hi there, I'm newby in perl and XML. I can read and parse Xml with XML-Node upper XML::Parser, but how can I create XML tags and pack my individual data in it then send through socket. PLZ lead me :) Thanks in Advance. (1 Reply)
Discussion started by: Zaxon
1 Replies

5. Shell Programming and Scripting

String handling is not working inside if loop

Hi All, I am comparing two strings inside an if condition if the strings are same then it should go inside the loop else it should execute code given in else part. But there is a but inside my script Even if the if condition is true it is not going inside the loop also it is executing... (4 Replies)
Discussion started by: usha rao
4 Replies

6. Shell Programming and Scripting

file handling in perl without using system command

Hi , Is there any way to achieve following using perl program (i.e without using system command). 1.system ("echo 'test' > /usr/spool/ship.csv"); 2.system ("cat /usr/ajay_test* >> /usr/spool/RAM/work/patil.csv"); 3.system("> /usr/spool/ajay.txt"); e.g for system("rm -f... (1 Reply)
Discussion started by: ajaypatil_am
1 Replies

7. Shell Programming and Scripting

Handling Parameters in Perl

Hi All, I'm pretty new to the forum and also to UNIX. I have a requirement for which I need some help. I have a script (example.script) where I get user inputs using the read command. I would need to pass the read-fetched input to a perl command (explained below) in my script. The part which... (3 Replies)
Discussion started by: bharath.gct
3 Replies

8. Programming

Perl help for file handling

$# some text $$ some text $@ some text $$. some text Mg1 some text Mg2 some text . . . Mg10 some text The above 10 lines are to be extracted except the lines starting from $#,$$.,... (4 Replies)
Discussion started by: baig.abdul
4 Replies

9. Shell Programming and Scripting

PERL error handling

I have a PERL command line embedded in a UNIX script. The script doesn't handle errors coming out of this command. I'm processing large files and occassionally I run out of disk space and end up with half a file. perl -p -e 's/\n/\r\n/g' < TR_TMP_$4 > $4 How do I handle errors coming out... (1 Reply)
Discussion started by: OTChancy
1 Replies

10. Shell Programming and Scripting

Perl file handling error

Hi, I am reading and file and writting each word to other file. where I have used array to store the data. I am getting below error as "Use of uninitialized value in concatenation (.) or string at customize_split_raw.pl line 51, <IN_FILE> " Where my line 51 code is 50 foreach... (8 Replies)
Discussion started by: Beginer123
8 Replies
ReadBackwards(3pm)					User Contributed Perl Documentation					ReadBackwards(3pm)

NAME
File::ReadBackwards.pm -- Read a file backwards by lines. SYNOPSIS
use File::ReadBackwards ; # Object interface $bw = File::ReadBackwards->new( 'log_file' ) or die "can't read 'log_file' $!" ; while( defined( $log_line = $bw->readline ) ) { print $log_line ; } # ... or the alternative way of reading until ( $bw->eof ) { print $bw->readline ; } # Tied Handle Interface tie *BW, 'File::ReadBackwards', 'log_file' or die "can't read 'log_file' $!" ; while( <BW> ) { print ; } DESCRIPTION
This module reads a file backwards line by line. It is simple to use, memory efficient and fast. It supports both an object and a tied handle interface. It is intended for processing log and other similar text files which typically have their newest entries appended to them. By default files are assumed to be plain text and have a line ending appropriate to the OS. But you can set the input record separator string on a per file basis. OBJECT INTERFACE
These are the methods in "File::ReadBackwards"' object interface: new( $file, [$rec_sep], [$sep_is_regex] ) "new" takes as arguments a filename, an optional record separator and an optional flag that marks the record separator as a regular expression. It either returns the object on a successful open or undef upon failure. $! is set to the error code if any. readline "readline" takes no arguments and it returns the previous line in the file or undef when there are no more lines in the file. If the file is a non-seekable file (e.g. a pipe), then undef is returned. getline "getline" is an alias for the readline method. It is here for compatibility with the IO::* classes which has a getline method. eof "eof" takes no arguments and it returns true when readline() has iterated through the whole file. close "close" takes no arguments and it closes the handle tell "tell" takes no arguments and it returns the current filehandle position. This value may be used to seek() back to this position using a normal file handle. get_handle "get_handle" takes no arguments and it returns the internal Perl filehandle used by the File::ReadBackwards object. This handle may be used to read the file forward. Its seek position will be set to the position that is returned by the tell() method. Note that interleaving forward and reverse reads may produce unpredictable results. The only use supported at present is to read a file backward to a certain point, then use 'handle' to extract the handle, and read forward from that point. TIED HANDLE INTERFACE
tie( *HANDLE, 'File::ReadBackwards', $file, [$rec_sep], [$sep_is_regex] ) The TIEHANDLE, READLINE, EOF, CLOSE and TELL methods are aliased to the new, readline, eof, close and tell methods respectively so refer to them for their arguments and API. Once you have tied a handle to File::ReadBackwards the only I/O operation permissible is <> which will read the previous line. You can call eof() and close() on the tied handle as well. All other tied handle operations will generate an unknown method error. Do not seek, write or perform any other unsupported operations on the tied handle. LINE AND RECORD ENDINGS
Since this module needs to use low level I/O for efficiency, it can't portably seek and do block I/O without managing line ending conversions. This module supports the default record separators of normal line ending strings used by the OS. You can also set the separator on a per file basis. The record separator is a regular expression by default, which differs from the behavior of $/. Only if the record separator is not specified and it defaults to CR/LF (e.g, VMS, redmondware) will it will be converted to a single newline. Unix and MacOS files systems use only a single character for line endings and the lines are left unchanged. This means that for native text files, you should be able to process their lines backwards without any problems with line endings. If you specify a record separator, no conversions will be done and you will get the records as if you read them in binary mode. DESIGN
It works by reading a large(8kb) block of data from the end of the file. It then splits them on the record separator and stores a list of records in the object. Each call to readline returns the top record of the list and if the list is empty it refills it by reading the previous block from the file and splitting it. When the beginning of the file is reached and there are no more lines, undef is returned. All boundary conditions are handled correctly i.e. if there is a trailing partial line (no newline) it will be the first line returned and lines larger than the read buffer size are handled properly. NOTES
There is no support for list context in either the object or tied interfaces. If you want to slurp all of the lines into an array in backwards order (and you don't care about memory usage) just do: @back_lines = reverse <FH>. This module is only intended to read one line at a time from the end of a file to the beginning. AUTHOR
Uri Guttman, uri@stemsystems.com COPYRIGHT
Copyright (C) 2003 by Uri Guttman. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.12.4 2011-07-15 ReadBackwards(3pm)
All times are GMT -4. The time now is 08:35 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy