|
Posted by Ben Bullock on March 25, 2008, 6:32 pm
Please log in for more thread options >
>
>
> > On Tue, 25 Mar 2008 11:59:23 +0000, John W. Krahn wrote:
> > > Ben Bullock wrote:
>
> > >> The foreach version seems to first read the whole of the file into an
> > >> array, and then go through it line by line:
>
> > > perldoc -q "What is the difference between a list and an array"
>
> > Found in /usr/local/lib/perl5/5.10.0/pod/perlfaq4.pod
> > What is the difference between a list and an array?
>
> > An array has a changeable length. A list does not.
>
> > If I had written "the foreach version reads the whole of the file into a
> > list", I would have contradicted this FAQ entry, which says I can't read
> > things into a list, because reading things into a list would change the
> > list's length, and "a list does not" have a changeable length.
>
> No, you're misunderstanding. A list is immutable *after it has been
> created*. Obviously you can create lists with any contents, otherwise
> you would be limited to using only lists compiled into perl. foreach
> accepts a list as argument and iterates over it; <> in list context
> (which is the real problem here) reads the entire file, splits it on
> newline (or rather $/), and returns a (newly created) list with the
> results. You can't modify the list after that: for an example where you
> can, see Tie::File, which reads a file into an *array* instead.
>
> Ben
|