Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IO::Handle::getline() doco should note an important difference from <$io> #7993

Closed
p5pRT opened this issue Jun 28, 2005 · 19 comments
Closed

Comments

@p5pRT
Copy link

p5pRT commented Jun 28, 2005

Migrated from rt.perl.org#36417 (status was 'resolved')

Searchable as RT36417$

@p5pRT
Copy link
Author

p5pRT commented Jun 28, 2005

From jm-perl.org@jmason.org

Created by jm-perl.org@jmason.org

This is a bug report for perl from jm-perl.org@​jmason.org,
generated with the help of perlbug 1.35 running under perl v5.8.4.

-----------------------------------------------------------------
perldoc IO​::Handle says​:

  $io->getline
  This works like <$io> described in "I/O Operators" in perlop except
  that it's more readable and can be safely called in a list context
  but still returns just one line.

However, there is one key difference​: "while (<$io>)" and "while
($io->getline)" ha ve different behaviour. If the last line of an input file
contains "0" (with no trailing newline), "while (<$io>)" will read it and
perform an iteration of the while loop, but "while ($io->getline)" will read
it, consider it a false value, and instead break out of the while loop.
I suggest changing the documentation to note this​:

  $io->getline
  This works like <$io> described in "I/O Operators" in perlop except
  that it's more readable and can be safely called in a list context
  but still returns just one line. Note that the correct analogue
  for "while (<$io>)" is "while (defined($io->getline))", not
  "while ($io->getline)", since an input line of "0" with no trailing
  newline will cause the loop to finish in the latter and not in
  either of the former.

Perl Info

Flags:
    category=docs
    severity=low

Site configuration information for perl v5.8.4:

Configured by Debian Project at Thu Mar 10 13:47:05 GMT 2005.

Summary of my perl5 (revision 5 version 8 subversion 4) configuration:
  Platform:
    osname=linux, osvers=2.6.8.1, archname=i386-linux-thread-multi
    uname='linux mcmurdo 2.6.8.1 #1 smp sat feb 26 13:03:40 utc 2005 i686 gnulinux '
    config_args='-Dusethreads -Duselargefiles -Dccflags=-DDEBIAN -Dcccdlflags=-fPIC -Darchname=i386-linux -Dprefix=/usr -Dprivlib=/usr/share/perl/5.8 -Darchlib=/usr/lib/perl/5.8 -Dvendorprefix=/usr -Dvendorlib=/usr/share/perl5 -Dvendorarch=/usr/lib/perl5 -Dsiteprefix=/usr/local -Dsitelib=/usr/local/share/perl/5.8.4 -Dsitearch=/usr/local/lib/perl/5.8.4 -Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/man3 -Dsiteman1dir=/usr/local/man/man1 -Dsiteman3dir=/usr/local/man/man3 -Dman1ext=1 -Dman3ext=3perl -Dpager=/usr/bin/sensible-pager -Uafs -Ud_csh -Uusesfio -Uusenm -Duseshrplib -Dlibperl=libperl.so.5.8.4 -Dd_dosuid -des'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=define use5005threads=undef useithreads=define usemultiplicity=define
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBIAN -fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-O2',
    cppflags='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBIAN -fno-strict-aliasing -I/usr/local/include'
    ccversion='', gccversion='3.3.5 (Debian 1:3.3.5-8ubuntu2)', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=4, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lgdbm -lgdbm_compat -ldb -ldl -lm -lpthread -lc -lcrypt
    perllibs=-ldl -lm -lpthread -lc -lcrypt
    libc=/lib/libc-2.3.2.so, so=so, useshrplib=true, libperl=libperl.so.5.8.4
    gnulibc_version='2.3.2'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
    cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:
    


@INC for perl v5.8.4:
    /etc/perl
    /usr/local/lib/perl/5.8.4
    /usr/local/share/perl/5.8.4
    /usr/lib/perl5
    /usr/share/perl5
    /usr/lib/perl/5.8
    /usr/share/perl/5.8
    /usr/local/lib/site_perl
    /usr/local/lib/perl/5.8.3
    /usr/local/share/perl/5.8.3
    /usr/local/lib/perl/5.8.2
    /usr/local/share/perl/5.8.2
    .


Environment for perl v5.8.4:
    HOME=/home/jm
    LANG=C
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/home/jm/bin:/sbin:/usr/sbin:/etc:/usr/etc:/usr/local/bin:/usr/local/bin:/usr/local/sbin:/sbin:/usr/sbin:/bin:/usr/bin:/usr/bin/X11:/usr/games:/home/jm/bin
    PERL_BADLANG (unset)
    SHELL=/bin/bash

@p5pRT
Copy link
Author

p5pRT commented Jun 28, 2005

From @schwern

On Tue, Jun 28, 2005 at 06​:25​:31PM -0000, Justin Mason wrote​:

However, there is one key difference​: "while (<$io>)" and "while
($io->getline)" ha ve different behaviour. If the last line of an input file
contains "0" (with no trailing newline), "while (<$io>)" will read it and
perform an iteration of the while loop, but "while ($io->getline)" will read
it, consider it a false value, and instead break out of the while loop.

Confirmed. while(<FH>) must have some sort of special case to consider
"0" true to avoid this sort of gotcha.

--
Michael G Schwern schwern@​pobox.com http​://www.pobox.com/~schwern
Just call me 'Moron Sugar'.
  http​://www.somethingpositive.net/sp05182002.shtml

@p5pRT
Copy link
Author

p5pRT commented Jun 28, 2005

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Jun 29, 2005

From @demerphq

On 6/29/05, Michael G Schwern <schwern@​pobox.com> wrote​:

On Tue, Jun 28, 2005 at 06​:25​:31PM -0000, Justin Mason wrote​:

However, there is one key difference​: "while (<$io>)" and "while
($io->getline)" ha ve different behaviour. If the last line of an input file
contains "0" (with no trailing newline), "while (<$io>)" will read it and
perform an iteration of the while loop, but "while ($io->getline)" will read
it, consider it a false value, and instead break out of the while loop.

Confirmed. while(<FH>) must have some sort of special case to consider
"0" true to avoid this sort of gotcha.

while (<FH>) {

is documented to be equivelent to

while (defined($_=<FH>)) {

in perlvar I/O Operators.

--
perl -Mre=debug -e "/just|another|perl|hacker/"

@p5pRT
Copy link
Author

p5pRT commented Jun 29, 2005

From @smpeters

On Wed, Jun 29, 2005 at 02​:25​:49PM +0200, demerphq wrote​:

On 6/29/05, Michael G Schwern <schwern@​pobox.com> wrote​:

On Tue, Jun 28, 2005 at 06​:25​:31PM -0000, Justin Mason wrote​:

However, there is one key difference​: "while (<$io>)" and "while
($io->getline)" ha ve different behaviour. If the last line of an input file
contains "0" (with no trailing newline), "while (<$io>)" will read it and
perform an iteration of the while loop, but "while ($io->getline)" will read
it, consider it a false value, and instead break out of the while loop.

Confirmed. while(<FH>) must have some sort of special case to consider
"0" true to avoid this sort of gotcha.

while (<FH>) {

is documented to be equivelent to

while (defined($_=<FH>)) {

in perlvar I/O Operators.

--
perl -Mre=debug -e "/just|another|perl|hacker/"

Then, how does the following sound to everyone...

Inline Patch
--- ext/IO/lib/IO/Handle.pm.old Sun Apr  4 08:32:35 2004
+++ ext/IO/lib/IO/Handle.pm     Wed Jun 29 08:34:48 2005
@@ -117,7 +117,10 @@

 This works like <$io> described in L<perlop/"I/O Operators">
 except that it's more readable and can be safely called in a
-list context but still returns just one line.
+list context but still returns just one line.  If used within a
+conditional statement, however, you will need to emulate the
+functionality of <$io> with C<defined($x = $io->getline)> where
+C<$x> is a scalar you have previously defined.

 =item $io->getlines


Steve Peters
steve@fisharerojo.org

@p5pRT
Copy link
Author

p5pRT commented Jun 29, 2005

From @tamias

On Wed, Jun 29, 2005 at 08​:37​:29AM -0500, Steve Peters wrote​:

Then, how does the following sound to everyone...

--- ext/IO/lib/IO/Handle.pm.old Sun Apr 4 08​:32​:35 2004
+++ ext/IO/lib/IO/Handle.pm Wed Jun 29 08​:34​:48 2005
@​@​ -117,7 +117,10 @​@​

This works like <$io> described in L<perlop/"I/O Operators">
except that it's more readable and can be safely called in a
-list context but still returns just one line.
+list context but still returns just one line. If used within a
+conditional statement, however, you will need to emulate the
+functionality of <$io> with C<defined($x = $io->getline)> where
+C<$x> is a scalar you have previously defined.

Why not just use $_ there? Not only does that emulate the actual behavior
of <$io>, but then you don't need the confusing note about $x.

Ronald

@p5pRT
Copy link
Author

p5pRT commented Jun 29, 2005

From @smpeters

On Wed, Jun 29, 2005 at 10​:24​:29AM -0400, Ronald J Kimball wrote​:

Why not just use $_ there? Not only does that emulate the actual behavior
of <$io>, but then you don't need the confusing note about $x.

Well, I was hoping to confuse ;).

Inline Patch
--- ext/IO/lib/IO/Handle.pm.old Sun Apr  4 08:32:35 2004
+++ ext/IO/lib/IO/Handle.pm     Wed Jun 29 10:43:47 2005
@@ -117,7 +117,9 @@

 This works like <$io> described in L<perlop/"I/O Operators">
 except that it's more readable and can be safely called in a
-list context but still returns just one line.
+list context but still returns just one line.  If used within a
+conditional statement, however, you will need to emulate the
+functionality of <$io> with C<defined($_ = $io->getline)>.

Steve Peters
steve@fisharerojo.org

@p5pRT
Copy link
Author

p5pRT commented Jun 29, 2005

From eric-amick@comcast.net

C<defined($_ = $io->getline)>

You mean

C<< defined($_ = $io->getline) >>

@p5pRT
Copy link
Author

p5pRT commented Jun 29, 2005

From @iabyn

On Wed, Jun 29, 2005 at 10​:47​:45AM -0500, Steve Peters wrote​:

This works like <$io> described in L<perlop/"I/O Operators">
except that it's more readable and can be safely called in a
-list context but still returns just one line.
+list context but still returns just one line. If used within a
+conditional statement, however, you will need to emulate the
  ^^^^^^^^^^^^^^^^^^^^^
+functionality of <$io> with C<defined($_ = $io->getline)>.

not within any old conditional statement, only as the sole contents of a
*while* condition.

--
This is a great day for France!
  -- Nixon at Charles De Gaulle's funeral

@p5pRT
Copy link
Author

p5pRT commented Jun 29, 2005

From @tamias

On Wed, Jun 29, 2005 at 11​:39​:32PM +0100, Dave Mitchell wrote​:

On Wed, Jun 29, 2005 at 10​:47​:45AM -0500, Steve Peters wrote​:

This works like <$io> described in L<perlop/"I/O Operators">
except that it's more readable and can be safely called in a
-list context but still returns just one line.
+list context but still returns just one line. If used within a
+conditional statement, however, you will need to emulate the
^^^^^^^^^^^^^^^^^^^^^
+functionality of <$io> with C<defined($_ = $io->getline)>.

not within any old conditional statement, only as the sole contents of a
*while* condition.

Or the conditional in a C-style for loop, I believe.

Ronald

@p5pRT
Copy link
Author

p5pRT commented Jun 29, 2005

From @iabyn

On Wed, Jun 29, 2005 at 06​:46​:49PM -0400, Ronald J Kimball wrote​:

not within any old conditional statement, only as the sole contents of a
*while* condition.

Or the conditional in a C-style for loop, I believe.

Indeed.

--
My get-up-and-go just got up and went.

@p5pRT
Copy link
Author

p5pRT commented Jun 30, 2005

From @smpeters

On Wed, Jun 29, 2005 at 11​:56​:10PM +0100, Dave Mitchell wrote​:

On Wed, Jun 29, 2005 at 06​:46​:49PM -0400, Ronald J Kimball wrote​:

not within any old conditional statement, only as the sole contents of a
*while* condition.

Or the conditional in a C-style for loop, I believe.

Indeed.

So, indeed, I hope that the third time is the charm...

Inline Patch
--- ext/IO/lib/IO/Handle.pm.old Sun Apr  4 08:32:35 2004
+++ ext/IO/lib/IO/Handle.pm     Wed Jun 29 21:46:44 2005
@@ -117,7 +117,9 @@

 This works like <$io> described in L<perlop/"I/O Operators">
 except that it's more readable and can be safely called in a
-list context but still returns just one line.
+list context but still returns just one line.  If used as the conditional
+within a C<while> or C-style C<for> loop, however, you will need to
+emulate the functionality of <$io> with C<defined($_ = $io->getline)>.

 =item $io->getlines

Steve Peters
steve@fisharerojo.org

@p5pRT
Copy link
Author

p5pRT commented Jun 30, 2005

From @rgs

Steve Peters wrote​:

On Wed, Jun 29, 2005 at 11​:56​:10PM +0100, Dave Mitchell wrote​:

On Wed, Jun 29, 2005 at 06​:46​:49PM -0400, Ronald J Kimball wrote​:

not within any old conditional statement, only as the sole contents of a
*while* condition.

Or the conditional in a C-style for loop, I believe.

Indeed.

So, indeed, I hope that the third time is the charm...

--- ext/IO/lib/IO/Handle.pm.old Sun Apr 4 08​:32​:35 2004
+++ ext/IO/lib/IO/Handle.pm Wed Jun 29 21​:46​:44 2005
@​@​ -117,7 +117,9 @​@​

This works like <$io> described in L<perlop/"I/O Operators">
except that it's more readable and can be safely called in a
-list context but still returns just one line.
+list context but still returns just one line. If used as the conditional
+within a C<while> or C-style C<for> loop, however, you will need to
+emulate the functionality of <$io> with C<defined($_ = $io->getline)>.

Thanks, applied (after having fixed the pod markup.)

@p5pRT p5pRT closed this as completed Jul 1, 2005
@p5pRT
Copy link
Author

p5pRT commented Jul 1, 2005

@iabyn - Status changed from 'open' to 'resolved'

@p5pRT
Copy link
Author

p5pRT commented Jul 5, 2005

From nick@ing-simmons.net

Justin Mason <perl5-porters@​perl.org> writes​:

# New Ticket Created by Justin Mason
# Please include the string​: [perl #36417]
# in the subject line of all future correspondence about this issue.
# <URL​: https://rt-archive.perl.org/perl5/Ticket/Display.html?id=36417 >

This is a bug report for perl from jm-perl.org@​jmason.org,
generated with the help of perlbug 1.35 running under perl v5.8.4.

-----------------------------------------------------------------
perldoc IO​::Handle says​:

$io->getline
This works like <$io> described in "I/O Operators" in perlop except
that it's more readable

Debatable - <$io> is idomatic perl

   and can be safely called in a list context
   but still returns just one line\.

And of course $io->getline is slower

The whole IO​::* thing was IMHO the biggest mistake Graham Barr & I made
in our contributions to perl.

But we were in that new-to-objects phase where everything had to be
an object.

However, there is one key difference​: "while (<$io>)" and "while
($io->getline)" ha ve different behaviour. If the last line of an input file
contains "0" (with no trailing newline), "while (<$io>)" will read it and
perform an iteration of the while loop, but "while ($io->getline)" will read
it, consider it a false value, and instead break out of the while loop.
I suggest changing the documentation to note this​:

$io->getline
This works like <$io> described in "I/O Operators" in perlop except
that it's more readable and can be safely called in a list context
but still returns just one line. Note that the correct analogue
for "while (<$io>)" is "while (defined($io->getline))", not
"while ($io->getline)", since an input line of "0" with no trailing
newline will cause the loop to finish in the latter and not in
either of the former.

Not that
  while (<$io>)
 
compiles to

  while (defined <$io>)

[Please do not change anything below this line]
-----------------------------------------------------------------
---
Flags​:
category=docs
severity=low
---
Site configuration information for perl v5.8.4​:

Configured by Debian Project at Thu Mar 10 13​:47​:05 GMT 2005.

Summary of my perl5 (revision 5 version 8 subversion 4) configuration​:
Platform​:
osname=linux, osvers=2.6.8.1, archname=i386-linux-thread-multi
uname='linux mcmurdo 2.6.8.1 #1 smp sat feb 26 13​:03​:40 utc 2005 i686 gnulinux '
config_args='-Dusethreads -Duselargefiles -Dccflags=-DDEBIAN -Dcccdlflags=-fPIC -Darchname=i386-linux -Dprefix=/usr -Dprivlib=/usr/share/perl/5.8 -Darchlib=/usr/lib/perl/5.8 -Dvendorprefix=/usr -Dvendorlib=/usr/share/perl5 -Dvendorarch=/usr/lib/perl5 -Dsiteprefix=/usr/local -Dsitelib=/usr/local/share/perl/5.8.4 -Dsitearch=/usr/local/lib/perl/5.8.4 -Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/man3 -Dsiteman1dir=/usr/local/man/man1 -Dsiteman3dir=/usr/local/man/man3 -Dman1ext=1 -Dman3ext=3perl -Dpager=/usr/bin/sensible-pager -Uafs -Ud_csh -Uusesfio -Uusenm -Duseshrplib -Dlibperl=libperl.so.5.8.4 -Dd_dosuid -des'
hint=recommended, useposix=true, d_sigaction=define
usethreads=define use5005threads=undef useithreads=define usemultiplicity=define
useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
use64bitint=undef use64bitall=undef uselongdouble=undef
usemymalloc=n, bincompat5005=undef
Compiler​:
cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBIAN -fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
optimize='-O2',
cppflags='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBIAN -fno-strict-aliasing -I/usr/local/include'
ccversion='', gccversion='3.3.5 (Debian 1​:3.3.5-8ubuntu2)', gccosandvers=''
intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
alignbytes=4, prototype=define
Linker and Libraries​:
ld='cc', ldflags =' -L/usr/local/lib'
libpth=/usr/local/lib /lib /usr/lib
libs=-lgdbm -lgdbm_compat -ldb -ldl -lm -lpthread -lc -lcrypt
perllibs=-ldl -lm -lpthread -lc -lcrypt
libc=/lib/libc-2.3.2.so, so=so, useshrplib=true, libperl=libperl.so.5.8.4
gnulibc_version='2.3.2'
Dynamic Linking​:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib'

Locally applied patches​:

---
@​INC for perl v5.8.4​:
/etc/perl
/usr/local/lib/perl/5.8.4
/usr/local/share/perl/5.8.4
/usr/lib/perl5
/usr/share/perl5
/usr/lib/perl/5.8
/usr/share/perl/5.8
/usr/local/lib/site_perl
/usr/local/lib/perl/5.8.3
/usr/local/share/perl/5.8.3
/usr/local/lib/perl/5.8.2
/usr/local/share/perl/5.8.2
.

---
Environment for perl v5.8.4​:
HOME=/home/jm
LANG=C
LANGUAGE (unset)
LD_LIBRARY_PATH (unset)
LOGDIR (unset)
PATH=/home/jm/bin​:/sbin​:/usr/sbin​:/etc​:/usr/etc​:/usr/local/bin​:/usr/local/bin​:/usr/local/sbin​:/sbin​:/usr/sbin​:/bin​:/usr/bin​:/usr/bin/X11​:/usr/games​:/home/jm/bin
PERL_BADLANG (unset)
SHELL=/bin/bash

@p5pRT
Copy link
Author

p5pRT commented Jul 5, 2005

From nick@ing-simmons.net

Michael G Schwern <schwern@​pobox.com> writes​:

On Tue, Jun 28, 2005 at 06​:25​:31PM -0000, Justin Mason wrote​:

However, there is one key difference​: "while (<$io>)" and "while
($io->getline)" ha ve different behaviour. If the last line of an input file
contains "0" (with no trailing newline), "while (<$io>)" will read it and
perform an iteration of the while loop, but "while ($io->getline)" will read
it, consider it a false value, and instead break out of the while loop.

Confirmed. while(<FH>) must have some sort of special case to consider
"0" true to avoid this sort of gotcha.

Yes. The kind of special case that is easy to do with operators.

@p5pRT
Copy link
Author

p5pRT commented Jul 5, 2005

From nick@ing-simmons.net

Steve Peters <steve@​fisharerojo.org> writes​:

On Wed, Jun 29, 2005 at 10​:24​:29AM -0400, Ronald J Kimball wrote​:

Why not just use $_ there? Not only does that emulate the actual behavior
of <$io>, but then you don't need the confusing note about $x.

Well, I was hoping to confuse ;).

--- ext/IO/lib/IO/Handle.pm.old Sun Apr 4 08​:32​:35 2004
+++ ext/IO/lib/IO/Handle.pm Wed Jun 29 10​:43​:47 2005
@​@​ -117,7 +117,9 @​@​

This works like <$io> described in L<perlop/"I/O Operators">
except that it's more readable and can be safely called in a
-list context but still returns just one line.
+list context but still returns just one line. If used within a
+conditional statement, however, you will need to emulate the
+functionality of <$io> with C<defined($_ = $io->getline)>.

By which time you have lost ALL the alleged benefits and are still paying
the speed penalty.
  - that can surely not be called "more readable" by anyone.
  - the explict $_ assign gives the thing a scalar context
  so list context "advantage" is history

The only reason for using ->getline is if you have some reason
for wanting to treat $io as an object.
e.g. if $io _might_ be a file handle OR some other object which
provides the ->getline interface.

Steve Peters
steve@​fisharerojo.org

@p5pRT
Copy link
Author

p5pRT commented Jul 5, 2005

From jm@jmason.org

-----BEGIN PGP SIGNED MESSAGE-----
Hash​: SHA1

Nick Ing-Simmons via RT writes​:

Steve Peters <steve@​fisharerojo.org> writes​:

On Wed, Jun 29, 2005 at 10​:24​:29AM -0400, Ronald J Kimball wrote​:

Why not just use $_ there? Not only does that emulate the actual behavior
of <$io>, but then you don't need the confusing note about $x.

Well, I was hoping to confuse ;).

--- ext/IO/lib/IO/Handle.pm.old Sun Apr 4 08​:32​:35 2004
+++ ext/IO/lib/IO/Handle.pm Wed Jun 29 10​:43​:47 2005
@​@​ -117,7 +117,9 @​@​

This works like <$io> described in L<perlop/"I/O Operators">
except that it's more readable and can be safely called in a
-list context but still returns just one line.
+list context but still returns just one line. If used within a
+conditional statement, however, you will need to emulate the
+functionality of <$io> with C<defined($_ = $io->getline)>.

By which time you have lost ALL the alleged benefits and are still paying
the speed penalty.
- that can surely not be called "more readable" by anyone.
- the explict $_ assign gives the thing a scalar context
so list context "advantage" is history

The only reason for using ->getline is if you have some reason
for wanting to treat $io as an object.
e.g. if $io _might_ be a file handle OR some other object which
provides the ->getline interface.

sometimes this is indeed the case -- e.g. in our case, it's in
SpamAssassin's "spamd", which may be dealing with a socket returned from
IO​::Socket​::UNIX, :​:SSL, or :​:INET->accept(). there's plenty of times
that dealing with a socket as an object can be useful, even given the
speed penalty.

- --j.
-----BEGIN PGP SIGNATURE-----
Version​: GnuPG v1.2.5 (GNU/Linux)
Comment​: Exmh CVS

iD8DBQFCyteHMJF5cimLx9ARAoSCAJ4z8Egylfx7Dmpe9H0iF/Xn6iP1pwCgheOz
9xuYCu3Fa51FFGESSUtFE1E=
=x3KK
-----END PGP SIGNATURE-----

@p5pRT
Copy link
Author

p5pRT commented Jul 5, 2005

From nick@ing-simmons.net

Justin Mason <jm@​jmason.org> writes​:

The only reason for using ->getline is if you have some reason
for wanting to treat $io as an object.
e.g. if $io _might_ be a file handle OR some other object which
provides the ->getline interface.

sometimes this is indeed the case -- e.g. in our case, it's in
SpamAssassin's "spamd", which may be dealing with a socket returned from
IO​::Socket​::UNIX, :​:SSL, or :​:INET->accept(). there's plenty of times
that dealing with a socket as an object can be useful, even given the
speed penalty.

Just to be clear​:

As far as I am aware all _sockets_ are real filehandles
(i.e. there is a reference to a GV which has an IO which contains
C level PerlIO * object - that is more then enough indirections already...).

And if (reference to) IO (or GV) is "blessed" to make it an object
that doesn't stop you doing <$socket>.

What I mean't was something more abstract like​:

{package Fake;

sub new {
  my ($class,@​list) = @​_ ;
  return bless \@​list,$class;
}

sub getline
{
my $self = shift;
return shift(@​$self);
}

}

package main;

sub useit
{
my $handle = shift; # some source of lines
my $data = $handle->getline;
print $data;
}

# IO​::Handle
useit( \*STDIN );

# Something else entirely
useit( Fake->new("Whatever") );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant