develooper Front page | perl.perl5.porters | Postings from August 2008

Re: Near-FMTEYEWTK instructorial on ties, handles, and methods (was: How to tell whether readline got an error or EOF)

Thread Previous | Thread Next
From:
Tom Christiansen
Date:
August 3, 2008 10:41
Subject:
Re: Near-FMTEYEWTK instructorial on ties, handles, and methods (was: How to tell whether readline got an error or EOF)
Message ID:
20896.1217785266@chthon
In-Reply-To: Message from Ben Morrow <ben@morrow.me.uk> 
   of "Sun, 03 Aug 2008 05:14:42 BST." <iunfm5-uqc1.ln1@osiris.mauzo.dyndns.org> 

> I apologise for the bluntness. However, I *did* test what I was
> saying before I posted; specifically, I tested

>     $ perl -MIO::Handle -le'print CALC->error'
>     Can't locate object method "error" via package "CALC" (perhaps you
>     forgot to load "CALC"?) at -e line 1.
>     $ perl -MIO::Handle -le'open CALC, "foo"; print CALC->error'
>     -1

> and then I tested

>     $ perl -MIO::Handle -le'{package Foo; sub error { "FOO" }}
>         open CALC, "foo"; bless \*CALC, "Foo"; print CALC->error'
>     -1
>     $ perl -MIO::Handle -le'{package Foo; sub error { "FOO" }}
>         open CALC, "foo"; bless *CALC{IO}, "Foo"; print CALC->error'
>     FOO

> and it was a good thing I did, as I thought to start with the first
> would do the trick. It also works with tie:

>     $ perl -MIO::Handle -le'
>         {package Foo; sub TIEHANDLE { bless [] }}
>         tie *CALC, "Foo"; print CALC->error'
>     -1
>     $ perl -MIO::Handle -le'
>         {package Foo; sub TIEHANDLE { bless [] }}
>         {package Bar; sub error { "BAR" }}
>         tie *CALC, "Foo"; bless *CALC{IO}, "Bar";
>         print CALC->error'
>     BAR

Yes, curious.  The interception happens elsewhen.

>> The compiler generates different code, but it's hardly a matter of
>> whether some global entity should be "in scope".  "Visible at
>> compilation time" might work better, but I'm not going to do your
>> work for you.

> 'Visible at method-call time' is exactly what I meant. I should have
> been clearer.

>> Only barewords have this compilation gotcha.  *Indirect* handles
>> used in the dative slot do *not* have this issue, and they are
>> welcome to be globals, not even scoped variables.
>> 
>>     % perl -MIO::Handle -E '$tmp = *STDOUT{IO}; say $tmp $tmp->error()...
>>     0

> Only the first needs to be a variable to make the parser behave.
> Perl seems to be assuming no-one would ever want to make a method
> call like

>     $meth Class 1, 2, 3;

Yes, I long ago noticed that.  Probably just as well.  

>> Yes, I'm testy.  You'd be, too, if someone mis/hypercorrected you
>> without ever even checking their results first!
>> 
>> Don't you think?

> Again: I apologise for my earlier bluntness.

Accepted.  

And thank you for pointing out the real code in (pp_)method_named.

--tom

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About