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

if( <HANDLE>) vs WHILE ( <HANDLE> ) #356

Closed
p5pRT opened this issue Aug 8, 1999 · 3 comments

Comments

@p5pRT
Copy link
Collaborator

@p5pRT p5pRT commented Aug 8, 1999

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

Searchable as RT1190$

@p5pRT

This comment has been minimized.

Copy link
Collaborator Author

@p5pRT p5pRT commented Aug 8, 1999

From bbense@networking.stanford.edu

The following program illustrates my problem. Why does

if ( <FOO> )

leave $_ empty ?

Using -w gives this warning
Use of uninitialized value at test.pl line 14, <FOO> chunk 1.

#/usr/bin/perl

open(FOO,'>./foo') ;

print FOO "valueA\tvalueB\n" ;

close (FOO);

# this does not work

open(FOO,"<./foo") or die "open foo failed" ;

if ( <FOO> ) {
  print "​:$_​:" ;
} else {
  die "bad foo" ;
}

close (FOO) ;

# this does work

open(FOO,"<./foo") or die "open foo failed" ;

while ( <FOO> ) {
  print "​:$_​:" ;
}

close (FOO) ;

Perl Info


Site configuration information for perl 5.00503:

Configured by bbense at Thu Apr  1 09:45:08 PST 1999.

Summary of my perl5 (5.0 patchlevel 5 subversion 3) configuration:
  Platform:
    osname=solaris, osvers=2.5.1, archname=sun4-solaris-thread
    uname='sunos telemark.stanford.edu 5.5.1 generic_103640-21 sun4u sparc sunw,ultra-1 '
    hint=previous, useposix=true, d_sigaction=define
    usethreads=define useperlio=define d_sfio=define
  Compiler:
    cc='gcc', optimize='-O -g', gccversion=2.8.1
    cppflags='-D_REENTRANT -DDEBUGGING -I/usr/local/include -I/usr/pubsw/include'
    ccflags ='-D_REENTRANT -DDEBUGGING -I/usr/local/include -I/usr/pubsw/include'
    stdchar='unsigned char', d_stdstdio=define, usevfork=false
    intsize=4, longsize=4, ptrsize=4, doublesize=8
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
    alignbytes=8, usemymalloc=y, prototype=define
  Linker and Libraries:
    ld='gcc', ldflags ='-L/usr/local/lib -L/usr/pubsw/lib'
    libpth=/usr/local/lib /usr/pubsw/lib /lib /usr/lib /usr/ccs/lib
    libs=-lsfio -lsocket -lnsl -lgdbm -ldb -ldl -lm -lposix4 -lpthread -lc -lcrypt -lbsd
    libc=/lib/libc.so, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
    cccdlflags='-fPIC', lddlflags='-G -L/usr/local/lib -L/usr/pubsw/lib'

Locally applied patches:
    


@INC for perl 5.00503:
    /usr/local/lib/perl5/5.00503/sun4-solaris-thread
    /usr/local/lib/perl5/5.00503
    /usr/local/lib/perl5/site_perl/5.005/sun4-solaris-thread
    /usr/local/lib/perl5/site_perl/5.005
    .


Environment for perl 5.00503:
    HOME=/opt/users/bbense
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/opt/users/bbense/bin:/usr/bin:.:/usr/local/bin:/usr/newsw/bin:/usr/pubsw/bin:/usr/pubsw/X/bin:/usr/newsw/X/bin:/networking/@sys/bin:/networking/share/bin:/usr/ccs/bin
    PERL_BADLANG (unset)
    SHELL=/bin/tcsh

@p5pRT

This comment has been minimized.

Copy link
Collaborator Author

@p5pRT p5pRT commented Aug 8, 1999

From [Unknown Contact. See original ticket]

Booker Bense <bbense@​networking.stanford.edu> writes​:

This is a bug report for perl from bbense@​telemark.stanford.edu,
generated with the help of perlbug 1.26 running under perl 5.00503.

-----------------------------------------------------------------
[Please enter your report here]

The following program illustrates my problem. Why does

if ( <FOO> )

leave $_ empty ?

Because you have not to assigned anything to $_.
From perlop.pod​:

"In scalar context, evaluating a filehandle in angle brackets yields
the next line from that file (the newline, if any, included), or
C<undef> at end-of-file or on error. When C<$/> is set to C<undef>
(sometimes known as file-slurp mode) and the file is empty, it
returns C<''> the first time, followed by C<undef> subsequently.

Ordinarily you must assign the returned value to a variable, but
there is one situation where an automatic assignment happens. If
and only if the input symbol is the only thing inside the conditional
of a C<while> statement (even if disguised as a C<for(;;)> loop),
the value is automatically assigned to the global variable $_,
destroying whatever was there previously. (This may seem like an
odd thing to you, but you'll use the construct in almost every Perl
script you write.) The $_ variables is not implicitly localized.
You'll have to put a C<local $_;> before the loop if you want that
to happen.

"
--
Nick Ing-Simmons

@p5pRT

This comment has been minimized.

Copy link
Collaborator Author

@p5pRT p5pRT commented Aug 8, 1999

From [Unknown Contact. See original ticket]

Booker Bense writes​:

The following program illustrates my problem. Why does
if ( <FOO> )
leave $_ empty ?

Because while() is the only place that <> is special. See the
perlop manpage​:

  Ordinarily you must assign the returned value to a
  variable, but there is one situation where an automatic
  assignment happens. If and only if the input symbol is
  the only thing inside the conditional of a while statement
  (even if disguised as a for(;;) loop), the value is
  automatically assigned to the global variable $_,

Nat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.