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
Gedcom::Record(3pm)					User Contributed Perl Documentation				       Gedcom::Record(3pm)

NAME
Gedcom::Record - a module to manipulate Gedcom records Version 1.16 - 24th April 2009 SYNOPSIS
use Gedcom::Record; my $record = tag_record("CHIL", 2); my @records = tag_record("CHIL"); my @recs = $record->record("birth"); my @recs = $record->record("birth", "date"); my $rec = $record->record("birth date"); my $rec = $record->record(["birth", 2], "date"); my @recs = $record->get_record("birth"); my $val = $record->get_value; my @vals = $record->get_value("date"); my @vals = $record->get_value("birth", "date"); my $val = $record->get_value("birth date"); my $val = $record->get_value(["birth", 2], "date"); my $rec = $record->add("birth date", "1 Jan 2000"); my $rec = $record->set("birth date", "2 Jan 2000"); $self->parse($record, $grammar); $record->collect_xrefs($callback); my $xref = $record->resolve_xref($record->{value}); my @famc = $record->resolve $record->get_value("FAMC"); $record->resolve_xrefs($callback); $record->unresolve_xrefs($callback); return 0 unless $record->validate_semantics; $record->normalise_dates($format); $record->renumber($args); print $record->summary, " "; $record->delete_record($sub_record); DESCRIPTION
A selection of subroutines to handle records in a gedcom file. Derived from Gedcom::Item. HASH MEMBERS
Some of the more important hash members are: $record->{new_xref} Used by renumber(). $record->{recursed} Used by renumber(). METHODS
tag_record my $record = tag_record("CHIL", 2); my @records = tag_record("CHIL"); Get specific sub-records from the record. This function is identical to Gedcom::Item::get_item(). The arguments are the name of the tag, and optionally the count. In scalar context, returns the sub-record, or undef if it doesn't exist. In array context, returns all sub-records matching the specified tag. record my @recs = $record->record("birth"); my @recs = $record->record("birth", "date"); my $rec = $record->record("birth date"); my $rec = $record->record(["birth", 2], "date"); my @recs = $record->get_record("birth"); Retrieve a record. The get_record() function is identical to the record() function. In scalar context, record() returns the specified record, or undef if there is none. In list context, record() returns all the specified records. Records may be specified by a list of strings. Each string is either a Gedcom tag or a description. Starting from the first string in the list, specified records are retrieved. Then from those records, records specified by the next string in the list are retrieved. This continues until all strings from the list have been used. In list context, all specified records are retrieved. In scalar context, only the first record is retrieved. If a record other than the first is wanted, then instead of passing a string, a reference to an array containing the string and a count may be passed. Instead of specifying a list of strings, it is possible to specify a single space separated string. This can make the interface nicer. get_value my $val = $record->get_value; my @vals = $record->get_value("date"); my @vals = $record->get_value("birth", "date"); my $val = $record->get_value("birth date"); my $val = $record->get_value(["birth", 2], "date"); Retrieve a record's value. If arguments are specified, record() is first called with those arguments, and the values of those records are returned. add my $rec = $record->add("birth date", "1 Jan 2000"); Add a new record. Add a new record ($rec) as a sub-item of $record. Set its value to the last argument given. The first arguments may be specified as for record(). A new record will always be created for the last argument, and for any arguments for which the count is explicitly set to zero. If the new record does not take a value then do not supply one. This does mean that you cannot use the function with many arguments if the last one is a scalar, but not a value. In this case either specify the last argument as ["arg", 0], or add undef as the last argument. set my $rec = $record->set("birth date", "2 Jan 2000"); Set the value of a record. This is the same as add(), with the exception that a new record is not created for the last argument. parse $self->parse($record, $grammar); Parse a Gedcom record. Match a Gedcom::Record against a Gedcom::Grammar. Warn of any mismatches, and associate the Gedcom::Grammar with the Gedcom::Record as $record->{grammar}. Do this recursively. collect_xrefs $record->collect_xrefs($callback); Recursively collect all the xrefs. Called by Gedcom::collect_xrefs. $callback is not used yet. resolve_xref my $xref = $record->resolve_xref($value); See Gedcom::resolve_xrefs() resolve my @famc = $record->resolve $record->tag_value("FAMC"); For each argument, either return it or, if it an xref, return the referenced record. resolve_xrefs $record->resolve_xrefs($callback); See Gedcom::resolve_xrefs() unresolve_xrefs $record->unresolve_xrefs($callback); See Gedcom::unresolve_xrefs() validate_semantics return 0 unless $record->validate_semantics; Validate the semantics of the Gedcom::Record. This performs a number of consistency checks, but could do even more. Returns true iff the Record is valid. normalise_dates $record->normalise_dates($format); Change the format of all dates in the record. See the documentation for Gedcom::normalise_dates renumber $record->renumber($args); Renumber the record. See Gedcom::renumber(). child_value NOTE - This function is deprecated - use tag_value instead. my $child = $record->child_value("NAME"); child_values NOTE - This function is deprecated - use tag_value instead. my @children = $record->child_values("CHIL"); summary print $record->summary, " "; Return a line of text summarising the record. delete_record $record->delete_record($sub_record); Delete the specified sub-record from the record. Access functions All the Gedcom tag names can be used as function names. Depending on the context in which they are called, the functions return either an array of the specified sub-items, or the first specified sub-item. The descriptions of the tags, with spaces replaced by underscores, can also be used as function names. The function names can be of either, or mixed case. Unless you use the tag name, in either case, or the description in lower case, the function will not be pre- declared and you will need to qualify it or "use subs". perl v5.14.2 2012-04-12 Gedcom::Record(3pm)
All times are GMT -4. The time now is 08:43 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy