SPUG: FILEHANDLE - What am I missing here.

Brian Hatch spug at ifokr.org
Thu Jan 2 23:27:57 CST 2003

> sub1 then I dutifully get the first 200 lines of the file, 100 each from
> sub2 and sub3. What am I missing here. Why does <FILE> seem to close (or
> go out of scope) after sub1. And is there a way to do what I am trying
> to do? 

looks like sub1 calls getline:

>   foreach(&getline){

and getline looks like this

> sub getline{<FILE>;}

Since getline returns an array (all the lines of FILE) to
your foreach loop, there's nothing left in FILE.  Here's

> sub sub1{
>   foreach(&getline){
>     print;
>     print "SUB3";
>     return if(/sometext/);
>   }
> }

Even though you may return before processing all the lines
in the foreach, getline has already snagged all lines.  There's
nothing left in FILE by the time you call sub2, etc.

You should have getline return a scalar:

	sub getline { scalar <FILE> }

That way it gets a line, not all lines.

Brian Hatch                  Not one shred of
   Systems and                evidence supports
   Security Engineer          the notion that
http://www.ifokr.org/bri/     life is serious.

Every message PGP signed
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 240 bytes
Desc: not available
Url : http://mail.pm.org/archives/spug-list/attachments/20030102/bc2f4e22/attachment.bin

More information about the spug-list mailing list