Perl reference, what"s the difference
Perl reference, what"s the difference
am 27.11.2007 15:46:58 von xueweizhong
What causes the difference below?
#! /bin/perl -l
$h = *STDOUT{IO};
print $h "hello";
__END__
hello
#! /bin/perl -l
print *STDOUT{IO} "hello";
__END__
String found where operator expected at - line 2, near "} "hello""
(Missing operator before "hello"?)
syntax error at - line 2, near "} "hello""
Execution of - aborted due to compilation errors.
Thanks,
Todd
Re: Perl reference, what"s the difference
am 27.11.2007 15:54:22 von it_says_BALLS_on_your forehead
On Nov 27, 9:46 am, Todd wrote:
> What causes the difference below?
>
> #! /bin/perl -l
> $h = *STDOUT{IO};
> print $h "hello";
> __END__
>
> hello
>
> #! /bin/perl -l
> print *STDOUT{IO} "hello";
> __END__
>
> String found where operator expected at - line 2, near "} "hello""
> (Missing operator before "hello"?)
> syntax error at - line 2, near "} "hello""
> Execution of - aborted due to compilation errors.
>
> Thanks,
> Todd
print { *STDOUT{IO} } "Hello\n";
see perldoc -f print
....
Note that if you're storing FILEHANDLEs in an array,
or if you're using any other expression more complex
than a scalar variable to retrieve it, you will have
to use a block returning the filehandle value
instead:
print { $files[$i] } "stuff\n";
print { $OK ? STDOUT : STDERR } "stuff\n";
Re: Perl reference, what"s the difference
am 27.11.2007 16:03:26 von xueweizhong
Hi nolo,
> print { *STDOUT{IO} } "Hello\n";
> see perldoc -f print
> or if you're using any other expression more complex
> than a scalar variable to retrieve it, you will have
> to use a block returning the filehandle value
> instead:
Really nice. But do you ever know the difficulty in the compiler which
causes this intention?
-Todd
Re: Perl reference, what"s the difference
am 27.11.2007 16:06:14 von it_says_BALLS_on_your forehead
On Nov 27, 10:03 am, Todd wrote:
> Hi nolo,
>
> > print { *STDOUT{IO} } "Hello\n";
> > see perldoc -f print
> > or if you're using any other expression more complex
> > than a scalar variable to retrieve it, you will have
> > to use a block returning the filehandle value
> > instead:
>
> Really nice. But do you ever know the difficulty in the compiler which
> causes this intention?
>
nolo no know.
Re: Perl reference, what"s the difference
am 28.11.2007 15:58:00 von brian d foy
In article
<2cad07db-3665-457a-b3e5-a56c82714af8@s12g2000prg.googlegroups.com>,
Todd wrote:
> Hi nolo,
>
> > print { *STDOUT{IO} } "Hello\n";
> > see perldoc -f print
> > or if you're using any other expression more complex
> > than a scalar variable to retrieve it, you will have
> > to use a block returning the filehandle value
> > instead:
>
> Really nice. But do you ever know the difficulty in the compiler which
> causes this intention?
It's the Perl compiler having to guess what's coming next. The thingy
after the print could be the thing to print, or the filehandle to print
to.
print "Hello!\n";
print STDOUT "Hello!\n";
Since Perl 5.6, it's a bit more complicated because fileandles can be
stored in scalar variables. Notice there is no comma after $fh in the
print, just as there is no comma after the STDOUT.
open my($fh), ...;
print $message;
print $fh $message;
While parsign that, Perl has to guess that $fh is going to have a
filehandle reference in it even though it can't know that it actually
will at compile time. It looks either for a scalar variable or a
bareword to determine if I'm using a filehandle.
If I'm not using a scalar variable or a bareword and I have the
filehandle in something else, I need to give Perl the hint that the
thingy will have a filehandle reference by putting {} around the thing
that will have the filehandle reference. Remember, Perl is a dynamic
language, so that thingy won't have it's value until Perl runs it.
There's no type checking here.
print { $fh } "Hello!\n";
print { $handles{ $key } } "Hello!\n";
print { *STDOUT{IO} } "Hello!\n";
print { require IO::Interactive; IO::Interactive::interactive() }
"Hello!\n";
Good luck :)
Re: Perl reference, what"s the difference
am 30.11.2007 06:13:20 von xueweizhong
Hi brian,
> thingy will have a filehandle reference by putting {} around the thing
> that will have the filehandle reference. Remember, Perl is a dynamic
> language, so that thingy won't have it's value until Perl runs it.
> There's no type checking here.
Yes, dynamic type language can't do semantic expression checking at
compile time like C++'s template mechanism. So we have to make some
fix syntax to do the compiling. I should recall this from my memory:-)
Thanks, brian. Really clearer now.
Thanks,
Todd