brossard@sic.epfl.ch (Alain Brossard EPFL-SIC/SII) (04/07/91)
There is a feature which has just bitten me and made me lose a lot of hours and I think the man page should reflect this. When doing: print NEW <FILE>; It not only prints the next line of FILE as I wanted but prints all the rest of FILE. There is a paragraph in the 4.000 man page which relates to this (I think): If a <FILEHANDLE> is used in a context that is looking for an array, an array consisting of all the input lines is returned, one line per array element. It's easy to make a LARGE data space this way, so use with care. At least that is what I think it says, this is what I would call a feature in the bad sense of the word! Isn't Perl cryptic enough? Anyway I suggest adding: print <STDIN>; in the paragraph that says that all the following are equivalent: while ($_ = <STDIN>) { print; } while (<STDIN>) { print; } for (;<STDIN>;) { print; } print while $_ = <STDIN>; print while <STDIN>; I wanted to do print $_, <FILE>, <FILE>; and I had to do: print $_ = <FILE>; print $_ = <FILE>; The fact that I need to do an assignment seems counter intuitive! Alain Brossard
composer@chem.bu.edu (Jeff Kellem) (04/08/91)
In article <1268@sicsun.epfl.ch> brossard@sic.epfl.ch (Alain Brossard EPFL-SIC/SII) writes: > There is a feature which has just bitten me and made me > lose a lot of hours and I think the man page should reflect > this. When doing: > > print NEW <FILE>; > > It not only prints the next line of FILE as > I wanted but prints all the rest of FILE. There is a paragraph in > the 4.000 man page which relates to this (I think): > > If a <FILEHANDLE> is used in a context that is looking for > an array, an array consisting of all the input lines is > returned, one line per array element. It's easy to make a > LARGE data space this way, so use with care. > > At least that is what I think it says, this is what I would > call a feature in the bad sense of the word! Isn't Perl > cryptic enough? Anyway I suggest adding: > > print <STDIN>; > > in the paragraph that says that all the following are equivalent: > > while ($_ = <STDIN>) { print; } > while (<STDIN>) { print; } > for (;<STDIN>;) { print; } > print while $_ = <STDIN>; > print while <STDIN>; > > I wanted to do print $_, <FILE>, <FILE>; and I had to do: > print $_ = <FILE>; print $_ = <FILE>; > The fact that I need to do an assignment seems counter intuitive! You don't have to do the assignment. You just need to put the filehandle into a scalar context. You can do so using scalar(). So, to do what you wanted, try: print $_, scalar(<FILE>), scalar(<FILE>); That should do what you expected. Enjoy... -jeff Jeff Kellem Internet: composer@chem.bu.edu
flee@cs.psu.edu (Felix Lee) (04/08/91)
Welcome to the wonderful world of scalar vs. array contexts. You can force a scalar context with the scalar() builtin: print scalar(<FILE>); Does anyone have a complete list of places where scalar and array contexts are forced? Here's an incomplete list: A = forced array context; S = forced scalar context @x = A; %x = A; $x = S; $x[S]; @x[A]; $x{S}; @x{A}; ($x, $y) = A; scalar(S); anylistfunction(A); anyotherfunction(S, ...); &subroutine(A); do subroutine(A); S binaryoperator S; unaryoperator S; -- Felix Lee flee@cs.psu.edu
worley@compass.com (Dale Worley) (04/08/91)
From: brossard@sic.epfl.ch (Alain Brossard EPFL-SIC/SII) print NEW <FILE>; It not only prints the next line of FILE as I wanted but prints all the rest of FILE. You've gotten bitten by the one truly non-obvious feature of Perl: You must always be aware of array vs. scalar context. 'print' provides an array context for its arguments. For better or worse, not knowing the contexts provided by all the operators one uses is as dangerous as not knowing the difference between $foo, $foo(1), and $foo{1}. Anyway I suggest adding: print <STDIN>; in the paragraph that says that all the following are equivalent: while ($_ = <STDIN>) { print; } while (<STDIN>) { print; } for (;<STDIN>;) { print; } print while $_ = <STDIN>; print while <STDIN>; Well, that paragraph is discussing flow-of-control constructions, not array handling. I wanted to do print $_, <FILE>, <FILE>; and I had to do: print $_ = <FILE>; print $_ = <FILE>; The fact that I need to do an assignment seems counter intuitive! You can do print scalar(<FILE>); to execute <FILE> in a scalar context, i.e., reading one line. Dale Dale Worley Compass, Inc. worley@compass.com -- I am successful because I am the only person in my city who is not heavily addicted to powerful narcotics.
merlyn@iwarp.intel.com (Randal L. Schwartz) (04/08/91)
In article <$dG&j&j1@cs.psu.edu>, flee@cs (Felix Lee) writes: | Does anyone have a complete list of places where scalar and array | contexts are forced? Page 378 of The Book begins "Appendix A: the Semi-Formal Description", from which a complete list of scalar and array contexts may be derived. That is, if someone has the time... I'm busy finishing up chat.pl :-). %_ = ("Just another Perl hacker,",0); print keys _; -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/