Prev: FAQ 6.15 How can I print out a word-frequency or line-frequency summary?
Next: FAQ 4.63 Why don't my tied hashes make the defined/exists distinction?
From: sreservoir on 30 Mar 2010 20:38 On 3/30/2010 8:33 PM, Tad McClellan wrote: > Ben Morrow<ben(a)morrow.me.uk> wrote: >> Quoth Justin C<justin.1003(a)purestblue.com>: > >>> $q = new CGI; > >> Method calls like >> >> new Class; >> method $object; >> method $object @args; # note the lack of comma after $object >> >> are in a form usually called 'indirect object syntax'[0]. This was added >> into Perl when objects were first introduced (5.000, I believe), > > > OP should see the docs section "Indirect Object Syntax" in perlobj.pod. > > But it contradicts Ben too: > > The other way to invoke a method is by using the so-called "indirect > object" notation. This syntax was available in Perl 4 long before > objects were introduced... out of curiosity, what did it do in perl4? -- "Six by nine. Forty two." "That's it. That's all there is." "I always thought something was fundamentally wrong with the universe."
From: Tad McClellan on 30 Mar 2010 22:16 sreservoir <sreservoir(a)gmail.com> wrote: > On 3/30/2010 8:33 PM, Tad McClellan wrote: >> OP should see the docs section "Indirect Object Syntax" in perlobj.pod. > out of curiosity, what did it do in perl4? Follow-upper should see the docs section "Indirect Object Syntax" in perlobj.pod too. :-) -- Tad McClellan email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/" The above message is a Usenet post. I don't recall having given anyone permission to use it on a Web site.
From: Ben Morrow on 31 Mar 2010 03:16 Quoth sreservoir <sreservoir(a)gmail.com>: > > this is, incidentally, slightly ridiculous. > > % perl -E'sub STDOUT::a { die "right blocking" } > say((*{STDOUT}{PACKAGE})->blocking);' > Can't locate object method "blocking" via package "main" at -e line 2. > > um. *STDOUT{PACKAGE} returns a string indicating which package *STDOUT is in, in this case "main". And no, it doesn't get preferentially treated as a package name, not even if you use *STDOUT::new{PACKAGE}. Ben
From: Ben Morrow on 31 Mar 2010 03:21 Quoth Tad McClellan <tadmc(a)seesig.invalid>: > Ben Morrow <ben(a)morrow.me.uk> wrote: > > > Method calls like > > > > new Class; > > method $object; > > method $object @args; # note the lack of comma after $object > > > > are in a form usually called 'indirect object syntax'[0]. This was added > > into Perl when objects were first introduced (5.000, I believe), > > OP should see the docs section "Indirect Object Syntax" in perlobj.pod. Yes. > But it contradicts Ben too: > > The other way to invoke a method is by using the so-called "indirect > object" notation. This syntax was available in Perl 4 long before > objects were introduced... (It's a long time since I've read perlobj.) Hmm, I suppose that's true, after a fashion. It's referring to the print FH "foo"; syntax that I said ungetc $FH "foo"; was supposed to be imitating. It doesn't apply to anything other than a very small handful of builtins (are there actually any others?). Ben
From: RedGrittyBrick on 31 Mar 2010 05:38
On 30/03/2010 22:10, Justin C wrote: > In article<h44887-vcl.ln1(a)osiris.mauzo.dyndns.org>, Ben Morrow wrote: >> >> Quoth sreservoir<sreservoir(a)gmail.com>: >>> On 3/29/2010 6:00 PM, RedGrittyBrick wrote: >>>> Dr.Ruud wrote: >>>>> RedGrittyBrick wrote: >>>>> >>>>>> my $query = new CGI; >>>>> >>>>> ITYM: >>>>> >>>>> my $cgi = CGI::->new(); >>>>> >>>> >>>> Yes, I did hesitate over that. In the end I copied what is in the >>>> documentation http://perldoc.perl.org/CGI.html#PROGRAMMING-STYLE. >>>> >>>> I've actually forgotten the pitfalls of the, er, deprecated syntax. >>>> Perusing perlobj, perlboot and perltoot didn't help me (though I wasn't >>>> very thorough). Is it anything to do with naming your constructor shazam >>>> and writing `my $instance = shazam OddObject'. Though it works, >>>> presumably as intended, I guess that sort of thing can confuse people& >>>> perl? >>>> >>>> So, why is `CGI::->new()' preferred over `CGI->new()' preferred over >>>> `new CGI()'? >>> >>> it isn't, unless someone's stupid enough to make&new or&CGI. it's >>> just less ambiguous: >> >> Having a 'sub new' in scope is not uncommon: >> >> package Foo; >> >> sub new { ... } >> >> sub init { my $x = CGI->new(...) } >> >> 1; >> >>> new CGI chokes if there is a&new or a&CGI. >> >> Not if you do it properly, that is >> >> new CGI (); >> >> rather than a bare >> >> new CGI; >> >> The latter is a little too ambiguous even for Perl. >> >>> CGI->new chokes if there is a&CGI. might do bad things if CGI isn't >>> require'd or use'd. >> >> You mean 'if the CGI->new method isn't defined'. >> >>> 'CGI'->new almost always does the right thing. >> >> use IO::Handle; >> warn "STDOUT"->blocking; >> >> Yes, this is 'the right thing' in this case, but not always. Just don't >> open a filehandle called 'CGI'. >> >>> CGI::->new is syntactic sugar for 'CGI'->new. >> >> For CGI->new, except it ignores any 'sub CGI'. >> >> Ben > > Well thanks a whole *huge* bunch, guys. Just when I start thinking I'm > starting to understand what I'm doing you go and confuse the hell out of > me! I spend a lot of my time in this group reading that I should refer > to documentation, and then you go and contradict the damn documentation! > OK, I accept that the module documentation isn't Perl documentation, but > that's a heavily used module, and it says quite clearly (unless I need > to update it): > > use CGI; > $q = new CGI; > print $q->header, .... > > If you can't explain it to me simply can you point me to some docs... I > see a Catch 22 here. > ---------------------------8<---------------------- #!/usr/bin/perl use strict; use warnings; use CGI; use Data::Dumper; foo(); sub CGI { print "Subroutine 'CGI' called\n"; } sub new { print "Subroutine 'new' called\n"; } sub foo { my $query; $query = new CGI; print Dumper(\$query); $query = 'CGI'->new; print Dumper(\$query); $query = CGI::->new; print Dumper(\$query); $query = CGI->new; print Dumper(\$query); } ---------------------------8<---------------------- :!perl testnew.pl Subroutine 'CGI' called Subroutine 'new' called $VAR1 = \'1'; $VAR1 = \bless( { '.parameters' => [], '.charset' => 'ISO-8859-1', '.fieldnames' => {} }, 'CGI' ); $VAR1 = \bless( { '.parameters' => [], '.charset' => 'ISO-8859-1', '.fieldnames' => {} }, 'CGI' ); Subroutine 'CGI' called Can't call method "new" without a package or object reference at testnew.pl line 16. -- RGB |