Perl : How is file handling working here?? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Perl : How is file handling working here??

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-12-2013
siddharthjindal siddharthjindal is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 12 July 2013, 7:23 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Perl : How is file handling working here??

Hi,

I have a perl script which is just reading from the file but what I would like to understand is how the counter is moving without using the loop in the script.

Below are the details:

Code:
$ more /tmp/abc.txt
This is 0
This is 1
This is 2
This is 3
This is 4
This is 5
This is 6
This is 7
This is 8
This is 9

Script to read the above file is as :

Code:
$ more file1.pl
#!/usr/bin/perl
#
sub readfile
{
     my ($rec,$INFO);
     $INF=shift;
     if (defined($rec = scalar <$INF>))
     {
          print $rec;
     }
}
$file='/tmp/abc.txt';
open(xyz,$file);
$i=0;
$tes=readfile(xyz);
$tes=readfile(xyz);

when I am executing the above script, it is throwing the output as :


Code:
$ perl file1.pl
This is 0
This is 1

How counter in the file is moving to next row when I am not moving it and I am also not using any loops anywhere? I would just like to understand that? Does that mean loops are not required for moving through the file or I am getting it wrong somewhere?

Thanks in advance for any help on this!


Moderator's Comments:
Use code tags please, see PM.

Last edited by zaxxon; 07-12-2013 at 06:51 AM.. Reason: code tags and indention
Sponsored Links
    #2  
Old 07-12-2013
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 26 October 2014, 10:18 AM EDT
Location: India
Posts: 1,832
Thanks: 25
Thanked 526 Times in 505 Posts
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;

Sponsored Links
    #3  
Old 07-12-2013
siddharthjindal siddharthjindal is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 12 July 2013, 7:23 AM EDT
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks lot for such a nice explanation Bala.

Appreciated!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl file handling error Beginer123 Shell Programming and Scripting 8 06-07-2012 03:50 AM
Perl help for file handling baig.abdul Programming 4 06-21-2011 04:39 AM
file handling in perl without using system command ajaypatil_am Shell Programming and Scripting 1 10-09-2009 02:11 PM
Perl revers File handling Zaxon Shell Programming and Scripting 2 01-18-2009 12:12 PM
file handling problem in perl...... vivekshankar UNIX for Dummies Questions & Answers 2 06-02-2005 08:23 PM



All times are GMT -4. The time now is 10:55 AM.