Prev: FAQ 3.14 How can I write a GUI (X, Tk, Gtk, etc.) in Perl?
Next: FAQ 1.13 Is it a Perl program or a Perl script?
From: Willem on 26 Jun 2010 04:40 John Kelly wrote: <snip> )<> or die "1 EOF\n"; )<> or die "2 EOF\n"; )<> or die "3 EOF\n"; )<> or die "4 EOF\n"; )<> or die "5 EOF\n"; )<> or die "6 EOF\n"; )<> or die "7 EOF\n"; )<> or die "8 EOF\n"; )<> or die "9 EOF\n"; <snip> ) produces output: ) )>data="one" )>6 EOF ) ) ) Which seems to prove that a bare <> is scalar context. No, it proves that the left-hand side of 'or' has scalar context. I'm pretty sure that a bare <> has void context, (which usually translates to scalar context). It is really a lot faster to change the line separator. Setting it to \2048 means that it will always read that many bytes, and undef'ing it would mean it will read the whole rest of the file. I don't know if <> is smart enough to recognize void context though, can probably be tested with a large file and a memory checker tool. SaSW, Willem -- Disclaimer: I am in no way responsible for any of the statements made in the above text. For all I know I might be drugged or something.. No I'm not paranoid. You all think I'm paranoid, don't you ! #EOT
From: John Kelly on 26 Jun 2010 09:48 On Sat, 26 Jun 2010 08:40:43 +0000 (UTC), Willem <willem(a)turtle.stack.nl> wrote: >John Kelly wrote: ><snip> >)<> or die "1 EOF\n"; >)<> or die "2 EOF\n"; >)<> or die "3 EOF\n"; >)<> or die "4 EOF\n"; >)<> or die "5 EOF\n"; >)<> or die "6 EOF\n"; >)<> or die "7 EOF\n"; >)<> or die "8 EOF\n"; >)<> or die "9 EOF\n"; ><snip> >) produces output: >) >)>data="one" >)>6 EOF >) >) >) Which seems to prove that a bare <> is scalar context. > >No, it proves that the left-hand side of 'or' has scalar context. Maybe. But when I do this: ><>; ><>; ><> or die "3 EOF\n"; ><> or die "4 EOF\n"; ><> or die "5 EOF\n"; ><> or die "6 EOF\n"; ><> or die "7 EOF\n"; ><> or die "8 EOF\n"; ><> or die "9 EOF\n"; The output is the same, showing the first two <> diamonds read only one line each, thus proving a bare <> is NOT list context. >I'm pretty sure that a bare <> has void context, (which usually > translates to scalar context). From ISBN 0-596-00027-8: 2.7.3. Void Context Another peculiar kind of scalar context is the void context. This context not only doesn't care what the return value's type is, it doesn't even want a return value. From the standpoint of how functions work, it's no different from an ordinary scalar context. >It is really a lot faster to change the line separator. Yes, but I coded this test to determine what a bare <> does. >Setting it to \2048 means that it will always read that many bytes, Right. That's what I decided to use, although 4096. >and undef'ing it would mean it will read the whole rest of the file. Which I do NOT want. >I don't know if <> is smart enough to recognize void context though, >can probably be tested with a large file and a memory checker tool. No need for the big hammer, considering the text of 2.7.3, and my test code. -- Web mail, POP3, and SMTP http://www.beewyz.com/freeaccounts.php
From: C.DeRykus on 26 Jun 2010 09:58 On Jun 26, 1:40 am, Willem <wil...(a)turtle.stack.nl> wrote: > John Kelly wrote: > > <snip> > )<> or die "1 EOF\n"; > )<> or die "2 EOF\n"; > )<> or die "3 EOF\n"; > )<> or die "4 EOF\n"; > )<> or die "5 EOF\n"; > )<> or die "6 EOF\n"; > )<> or die "7 EOF\n"; > )<> or die "8 EOF\n"; > )<> or die "9 EOF\n"; > <snip> > ) produces output: > ) > )>data="one" > )>6 EOF > ) > ) > ) Which seems to prove that a bare <> is scalar context. > > No, it proves that the left-hand side of 'or' has scalar context. > > I'm pretty sure that a bare <> has void context, (which usually > translates to scalar context). Yes, bare <> does have void context: perl -we '$SIG{INT}=sub{exit};undef $/; <> ; END{print}' foo bar ^CUse of uninitialized value in print at -e line 1. whereas, with just scalar context: perl -we '$SIG{INT}=sub{exit};undef $/; 1 while <> ; END{print}' foo bar ^Cfoo > > It is really a lot faster to change the line separator. > Setting it to \2048 means that it will always read that many bytes, > and undef'ing it would mean it will read the whole rest of the file. > I don't know if <> is smart enough to recognize void context though, > can probably be tested with a large file and a memory checker tool. > So this'd be very fast. undef $/; <>; Unfortunately, like () = <>, there's potentially grave impact to a foot: perl -we '@ARGV=("big.txt"); undef $/; <>' Out of memory during "large" request ... -- Charles DeRykus
From: Ben Morrow on 26 Jun 2010 10:15 Quoth "C.DeRykus" <derykus(a)gmail.com>: > > Yes, bare <> does have void context: > > perl -we '$SIG{INT}=sub{exit};undef $/; <> ; END{print}' > foo > bar > ^CUse of uninitialized value in print at -e line 1. > > whereas, with just scalar context: > > perl -we '$SIG{INT}=sub{exit};undef $/; 1 while <> ; END{print}' > foo > bar > ^Cfoo That's not 'just scalar context'. <>-within-while is special-cased to assign to $_ (and check 'defined', rather than simply truth). Try perl -we '$SIG{INT}=sub{exit}; undef $/; $x = <>; END{print}' Ben
From: Tad McClellan on 26 Jun 2010 10:32
John Kelly <jak(a)isp2dial.com> wrote: > On Sat, 26 Jun 2010 08:40:43 +0000 (UTC), Willem ><willem(a)turtle.stack.nl> wrote: >>John Kelly wrote: >><>; > The output is the same, showing the first two <> diamonds read only one > line each, thus proving a bare <> is NOT list context. There is no "bare" <>. The <> in <>; is in a statement. The statement provides the context for <> (or for whatever else is in the stmt). >>I'm pretty sure that a bare <> has void context, (which usually ^^^^^^^ ^^^^^^^ >> translates to scalar context). > > From ISBN 0-596-00027-8: What is ISBN 0-596-00027-8? > 2.7.3. Void Context > Another peculiar kind of scalar context is the void context. This > context not only doesn't care what the return value's type is, it > doesn't even want a return value. From the standpoint of how functions > work, it's no different from an ordinary scalar context. Why do you quote that? That is, what point are you trying to make by quoting that? I almost called Willem on the "usually" part, until I re-read the "Context" section in perldoc perldata ... User-defined subroutines may choose to care whether they are being called in a void, scalar, or list context. void context always translates to scalar context for built-in functions. void context usually translates to scalar context for user-defined functions. So the nameless book you quote above is in error. It should have qualified "functions": From the standpoint of how built-in functions work, ... > Yes, but I coded this test to determine what a bare <> does. That's done easily enough with a simple one-liner: bash-4.0$ perl -we '<>' foo bash-4.0$ Since the program exited after only one input line, then the readline() must have been in scalar context, else it would have waited for more input rather than exit()ing. As we can see with: perl -we 'print <>' Note that here print() provides the (list) context for <>, and that the statement provides the (void) context for print(). >>I don't know if <> is smart enough to recognize void context though, Since it is a built-in, void context is treated the same as scalar context. -- Tad McClellan email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/" The above message is a Usenet post. I don't recall having given anyone permission to use it on a Web site. |