[tpm] Filehandle $fh opened only for output at ....

Shlomi Fish shlomif at shlomifish.org
Wed May 3 11:53:23 PDT 2017


Hi Zoffix and Tom!

On Tue, 02 May 2017 20:43:20 -0400
zoffix at zoffix.com wrote:

> Quoting Tom Legrady <legrady at gmail.com>:
> 
> > I've got an old, bad program running under Perl 5.6.1  
> 
> Wow! I didn't even know they made Perl that old! :)
> 
> > The code looks like
> >         while ( my $bug = <$fh> ) {  
> 
> As I recall the idiom is `while (defined(my $foo = <$fh>))`
>

From what I recall the defined is added implicitly in simple = <...> loops
like that (but may not be with more complex conditions). See:

«
shlomif at lap:~$ cat myprog.pl 
#!/usr/bin/perl

use strict;
use warnings;

open my $in, '<', 'file.txt' or die "Blah!";

while (my $l = <$in>)
{
    print "Got <$l>\n";
}
close $in;
shlomif at lap:~$ cat file.txt 
hello
0



shlomif at lap:~$ perl myprog.pl 
Got <hello
>
Got <0
>
Got <
>
Got <
>
Got <
>
shlomif at lap:~$ 
shlomif at lap:~$ echo -n $'hello\n0\n\n\n0' > file.txt
shlomif at lap:~$ perl myprog.pl 
Got <hello
>
Got <0
>
Got <
>
Got <
>
Got <0>
shlomif at lap:~$ 

»

Adding define there won't hurt though.

Regards,

	Shlomi Fish

> Are you *sure sure* it's actually opened for output? What's your open  
> line? It should have `<` for mode, not `>`
> 
> 
> Cheers,
> ZZ
> 
> 
> >
> >
> > Tom
> > _______________________________________________
> > toronto-pm mailing list
> > toronto-pm at pm.org
> > http://mail.pm.org/mailman/listinfo/toronto-pm  
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm


More information about the toronto-pm mailing list