HELP: (please help, wizards) Memory problems 
Author Message
 HELP: (please help, wizards) Memory problems

does not leak memory:  print while <INPUT>;
leaks memory:          print <INPUT> while not eof(INPUT);

I need the second version for simulating a 'tail'-like behavior.
How can I avoid growing processes with the second version?

PS: I use perl5.001e

--
   __ __ __  __  .__   __ __  . __ _   __  
  /_ /_ /_/ /_/ / /   /_ /_/ / /_ /_| /_ /   Beim Schlump 52
 /_//_ / | / | / /   /_// | / /_ /_| /_ /_   20144 Hamburg,Germany



Sat, 22 Nov 1997 03:00:00 GMT  
 HELP: (please help, wizards) Memory problems
: does not leak memory:  print while <INPUT>;
: leaks memory:          print <INPUT> while not eof(INPUT);

: I need the second version for simulating a 'tail'-like behavior.
: How can I avoid growing processes with the second version?

I found a workaround on my own:

print <INPUT>; # seems to be the problem
$_=<INPUT>; print; # works.

I am just curios: Does anybody know why?

--
   __ __ _   _   .__   __ _   . __ _   __  
  /_ /_ /_) /_) / /   /_ /_) / /_ /_) /_ /   Beim Schlump 52
 /_//_ / | / | / /   /_// | / /_ /_) /_ /_   20144 Hamburg,Germany



Thu, 27 Nov 1997 03:00:00 GMT  
 HELP: (please help, wizards) Memory problems

Quote:

>: does not leak memory:  print while <INPUT>;
>: leaks memory:          print <INPUT> while not eof(INPUT);

It's not really leaking; more disappearing at one go.
"print" asks for a list (array), and <INPUT> in a list context delivers
the whole file, one element per line - or tries to.

Quote:
>print <INPUT>; # seems to be the problem

This would print the whole file

Quote:
>$_=<INPUT>; print; # works.

This would print only one line, since $_ is a scalar.

        print scalar <INPUT>;

would do just one line, too, since the scalar operator forces scalar
context on the print.

The perlop(1) manual page quotes
        print while <INPUT>;
as a way to print a whole file in a memory-efficient (but not very
CPU-efficient) manner.

Ian



Fri, 28 Nov 1997 03:00:00 GMT  
 HELP: (please help, wizards) Memory problems

Quote:

>: does not leak memory:  print while <INPUT>;
>: leaks memory:          print <INPUT> while not eof(INPUT);

>: I need the second version for simulating a 'tail'-like behavior.
>: How can I avoid growing processes with the second version?

>I found a workaround on my own:

>print <INPUT>; # seems to be the problem
>$_=<INPUT>; print; # works.

>I am just curios: Does anybody know why?

'print' takes an array as its argument. When <INPUT> is evaluated in
an array context, it returns... an array! This array will keep growing
as you feed it more input. Your second line above is a good
workaround, as it forces <INPUT> to be evaluated in a scalar context.
-Doug

--
Doug McNaught       Systems Integrator        Towson State University


            WWW Home Page: http://www.towson.edu/~doug/



Fri, 28 Nov 1997 03:00:00 GMT  
 HELP: (please help, wizards) Memory problems

Quote:

>leaks memory:          print <INPUT> while not eof(INPUT);

>I need the second version for simulating a 'tail'-like behavior.

[ Others have mentioned that it isn't strictly leaking memory. ]

You'll need something a little trickier than this if you want an
effect similar to `tail -f'.  In particular, you need to clear the eof
condition on the filehandle, and you probably want to throw a sleep in
there, too.

Here's a simple `tail -f' in Perl.

    #! /usr/local/bin/perl5 -w

    $0 =~ s!.*/!!;

    die "$0: can't open $ARGV[0] for input: $!\n" unless open(IN, $ARGV[0]);

    seek(IN, 0, 2); # Seek to end of file.

    for (;;) {
            print <IN>;
            seek(IN, 0, 1); # Clear eof condition.
            sleep 1;
    }
--
Tim Goodwin        | "If you're used to paint-by-number, a blank canvas
Public IP Exchange | can be unsettling at first." -- Larry Wall



Sat, 29 Nov 1997 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Memory Problem, Please Help.

2. Help Please. Perl Win32, ODBC, Memory Problems

3. Please help, frustrating "Out of memory!"

4. Please help - Memory consumption

5. HELP PLEASE?? Not enough conventenal memory

6. Help...Help, install failed, please help!!

7. Help Help Please Help !

8. help help help please!!!!

9. please, help me, help me, help me.....

10. RFQ: Tk::Wizard - a wizard style widget for Perl/Tk

11. RFQ: Tk::Wizard - a wizard style widget for Perl/Tk

12. OUT of MEMORY (help, help.!!!!!!!)

 

 
Powered by phpBB® Forum Software