Click here to get back home

Perl read file eat up my memory...

 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
Perl read file eat up my memory... howa 06-01-2008
Posted by howa on June 1, 2008, 2:41 pm
Please log in for more thread options
Hi,

I have a SQL file (generated by mysql), it is over 2GB in size and its
few lines are:

cat -n test.sql

1 -- MySQL dump 8.23
2 --
3 ...
4 ...

Now, I have a program to read the file line by line:

#!/usr/bin/perl
use strict;

if ( scalar(@ARGV) == 1 ) {

        my $filename = ( $ARGV[0] );
        open(IN_FILE, $filename) or die("Could not open the file.");

        my $line;

        foreach $line (<IN_FILE>) {

                        print "Test";
                        exit;

        }
        close(IN_FILE);

}

Suppose the file will read the first line and exit, however, it was
halt and eating my memory forever, no output for print "Test" or exit
at all.


Any idea?

Howard.

Posted by A. Sinan Unur on June 1, 2008, 2:52 pm
Please log in for more thread options
f40ba0c2bfd2@y22g2000prd.googlegroups.com:

> Now, I have a program to read the file line by line:

No you don't.

> my $line;
>
> foreach $line (<IN_FILE>) {

Using a for loop here will read the entire file into memory first, before
it can begin to process the lines.

Instead, use a while loop which will indeed read the file line-by-line:

while ( my $line = <IN_FILE> ) {

Sinan

--
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/

Posted by Jürgen Exner on June 1, 2008, 3:27 pm
Please log in for more thread options
>Now, I have a program to read the file line by line:
>
>        foreach $line (<IN_FILE>) {

But you don't. You are creating a list of all the lines in the file,
then looping through that list.

You are probably looking for
        while (my $line= <IN_FILE>) {

jue

Posted by howa on June 1, 2008, 10:19 pm
Please log in for more thread options
Hi,

On 6=D4=C22=C8=D5, =C9=CF=CE=E73=95r27=B7=D6, J=A8=B9rgen Exner <jurge...@ho=
tmail.com> wrote:
> >Now, I have a program to read the file line by line:
>
> > foreach $line (<IN_FILE>) {
>
> But you don't. You are creating a list of all the lines in the file,
> then looping through that list.
>
> You are probably looking for
> while (my $line=3D <IN_FILE>) {
>
> jue

Oh..., I google for "perl read file line by line" and return "http://
www.cs.cf.ac.uk/Dave/PERL/node241.html", I overlooked the code in that
page are not reading line by line in fact!

Thanks guys!

Howard


Posted by Uri Guttman on June 2, 2008, 12:17 am
Please log in for more thread options

h> Oh..., I google for "perl read file line by line" and return "http://
h> www.cs.cf.ac.uk/Dave/PERL/node241.html", I overlooked the code in that
h> page are not reading line by line in fact!

that web page is purported to be a book (dunno if a dead tree version
was printed) but it is up there with the other crappy web tutorials out
there. i wish i had the resources to catalog and critique them. so many
to choose from.

this one is very idiosyncratic with short pages with weak examples and
even worse and wrong explanations.


from:
http://www.cs.cf.ac.uk/Dave/PERL/node23.html#SECTION00411000000000000000

        You define scalar variables by assigning a value (number or
        string) to it.

hmm. that seems to mean my $foo is not defining (really declaring) a scalar.

        * It is a good idea to declare all variables together near the
top of the program.

that is actually backwards.

and nothing is mentioned about all the other scalar value types in this
page. in fact this page is more about the values than about the title
'scalar variables'. the author doesn't seem to know the difference
between the container and its contents.

from: http://www.cs.cf.ac.uk/Dave/PERL/node24.html

        String Scalar Variables

that page title is so wrong.

from:
        http://www.cs.cf.ac.uk/Dave/PERL/node27.html

        Literal Arrays

        Arrays can be defined literally in Perl code by simply enclosing
        the array elements in parentheses and separating each array
        element with a comma.

For example

(1, 2, 3)
("fred", "albert")
() # empty array (zero elements)


wow. i can't even begin to comment on that balderdash. i have never
heard of the term literal array (we call them lists). don't you love
technical authors who invent new terms instead of using the well known
and accepted ones?

from:
http://www.cs.cf.ac.uk/Dave/PERL/node20.html#SECTION00322100000000000000

        Example: Back-Quoted Strings

        It might be argued that back-quoted strings are not really a
        data type. That's because Perl uses back-quoted strings to
        execute system commands. When Perl sees a back-quoted string, it
        passes the contents to Windows, UNIX, or whatever operating
        system you are using.

hmm. the author doesn't seem to realize that backticks also are double
quoted strings and are literals in that sense. the fact that they shell
out is a secondary effect.

        Let's see how to use the back-quoted string to display a
        directory listing of all text files in the perl5 directory:

        print `dir *.txt`;

wow, a useless example given readdir and glob!

from:
        http://www.cs.cf.ac.uk/Dave/PERL/node60.html#SECTION00910000000000000000


        $refFunction = \&function; & is a function location.

hmm. that calls the function ref. no location is returned.

of course he calls the tr/// op a regular expression. this is almost
universal in bad web tutes.


from:
http://www.cs.cf.ac.uk/Dave/PERL/node87.html#SECTION001180000000000000000

        The binding operators (=~ and !~) are used as search, modify,
        and translation operations and work on the $_ variable by
        default.

actually if you use a bind op you DON'T default to operating on $_ but
on the bound value. wow.

from:
http://www.cs.cf.ac.uk/Dave/PERL/node81.html#SECTION001140000000000000000

        The =~ lets you match against a specified target (rather than an
        environment variable $_. In CGI (see Later) and other
        application we frequently need to match against input
        name/values.

this is even wackier. $_ is an environment variable? what is he smoking?
and why does he have 2 pages covering =~?

from:
http://www.cs.cf.ac.uk/Dave/PERL/node79.html#SECTION001121000000000000000

        The control characters \d (digit), \s (space), \w (word
        character) can also be used. \D, \S, \W are the negations of
        \d\s\w (More on This Soon)

control characters??

ok, i had my fun. this is definitely the work of a dedicated author who
has never had anyone else comment on his work. i say this to my
students, job candidates, hiring clients, etc.: become part of the perl
community as you will learn more, gain valuable feedback and actually be
able to contribute something useful. writing (code or docs) in isolation
is never going to turn out well.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Similar ThreadsPosted
How to read big XML files using Perl - Memory Problem November 21, 2005, 3:37 pm
Determine read/write status of filehandles connected to memory objects. March 15, 2006, 11:40 pm
Read xml file and write data to a txt file using PERL.... urgent April 17, 2007, 4:17 pm
Perl takes a lot of memory when you just require a file February 15, 2007, 7:56 am
perl open/close file leaks memory?? September 27, 2007, 1:30 am
Help for the perl read file August 2, 2005, 2:48 pm
How to open and read pdf file in Perl. April 28, 2007, 9:51 am
How to read a pdf file using active perl? August 28, 2006, 9:21 am
How to read access database file with perl? November 16, 2004, 12:50 am
How do I read a GZipped UTF-8 file from Perl on Windows? May 29, 2007, 4:24 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap