mostek@motcid.UUCP (Frank B. Mostek) (03/28/91)
I can't get the + operator to work when I open a file for append. This
is supposed to open the file for read as well.
open(FILE, "+>>file");
while (<FILE>)
{
# Never gets in here
}
I've tried patch level 18 (3.0) and patch level 44. Is this a bug or
am I doing something wrong? I am on:
SunOS Release 4.0.3 (SDW403.STD3X.V1.1) #1: Sun Nov 12 19:52:18 CST 1989
Also, is there a way to grep a file within perl? Perl's grep needs a
list, I currently use system("grep...") which is much slower. I just
need to know if an expression occurs in a file. Is there an elegant
way to do this using Perl's constructs?
Thanx in advance
--
************************************************************
Frank Mostek uunet!motcid!amethyst!mostek
Software Consultant (708)632-7191
************************************************************
rbj@uunet.UU.NET (Root Boy Jim) (03/28/91)
In article <6086@beryl12.UUCP> mostek@motcid.UUCP (Frank B. Mostek) writes:
?I can't get the + operator to work when I open a file for append. This
?is supposed to open the file for read as well.
?
?open(FILE, "+>>file");
?
?while (<FILE>)
?{
? # Never gets in here
?}
When opening for append, the pointer is at the end.
There is nothing left to read. Seek to the beginning.
--
[rbj@uunet 1] stty sane
unknown mode: sane
akira@atson.asahi-np.co.jp (Akira Takiguchi) (03/29/91)
In article <126616@uunet.UU.NET> rbj@uunet.UU.NET (Root Boy Jim) writes: >In article <6086@beryl12.UUCP> mostek@motcid.UUCP (Frank B. Mostek) writes: >?I can't get the + operator to work when I open a file for append. This >?is supposed to open the file for read as well. >? >?open(FILE, "+>>file"); : >When opening for append, the pointer is at the end. >There is nothing left to read. Seek to the beginning. A bit of addition: perl's open uses stdio interface, not open(2). you know, fopen(3) behaves differently from open(2) when reading file opened in append mode [VAXC open does not implement this behaviour, BTW]. -- | Akira Takiguchi at ATSON, Inc. (a subsidiary of the Asahi Shimbun) | WAKO GINZA bldg. 8-10-4 Ginza Chuo-ku Tokyo 104 Japan | Phone +81 3 3289 7051 Fax +81 3 3289 7066 SORRY, EMAIL NOT AVAILABLE
allbery@NCoast.ORG (Brandon S. Allbery KB8JRR/AA) (04/03/91)
As quoted from <6086@beryl12.UUCP> by mostek@motcid.UUCP (Frank B. Mostek): +--------------- | Also, is there a way to grep a file within perl? Perl's grep needs a | list, I currently use system("grep...") which is much slower. I just | need to know if an expression occurs in a file. Is there an elegant | way to do this using Perl's constructs? +--------------- open(F,$file) || die "can't open $file: $!\n"; print grep(/re/, <F>); # <F> being in an array context, it puts the file into a temporary array and # then greps that. This behaves like grep(1) with one file argument and no # other options. # To simply determine the existence of an expression in a file: $found = grep(/re/, <F>); # grep returns an array of all the "true" values (the lines on which the /re/ # succeeded); in a scalar context, an array produces the length of the array, # which is 0 if no line contains /re/. Since 0 is false and non-0 is true, # this works as we expect. If you want to use the grep as an expression and # it might accidentally be taken as an array value, use "scalar" or one of # the other tricks (concatenate the empty string, add 0, etc.) to force a # scalar context. # Be warned that either of these on a large file will take a LOT of memory. # If this is a problem, do it one line at a time: $found = 0; $found += /re/ while $_ = <F>; # or while (<F>) { $found += /re/; } close(F); -- Me: Brandon S. Allbery Ham: KB8JRR/AA on 2m, 220, 440, 1200 Internet: allbery@NCoast.ORG (QRT on HF until local problems fixed) America OnLine: KB8JRR // Delphi: ALLBERY AMPR: kb8jrr.AmPR.ORG [44.70.4.88] uunet!usenet.ins.cwru.edu!ncoast!allbery KB8JRR @ WA8BXN.OH