Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

indirect object in camel book

by Anarion (Hermit)
on Oct 05, 2001 at 20:26 UTC ( [id://117035]=perlquestion: print w/replies, xml ) Need Help??

Anarion has asked for the wisdom of the Perl Monks concerning the following question:

In the Camel Book, the third edition and i found this:
"
12.3
use ElvenRing;
require ElvenRing;

Either of these declarations ensures that Perl knows ElvenRing is a module name, which forces any bare name like new before the class name ElvenRing to be interpreted as a method call, even if you happen to have declared a new subroutine of your own in the current package. People don't generally get into trouble with indirect objects unless they start cramming multiple classes into the same file, in which case Perl might not know that a particular package name was supposed to be a class name. People who name subroutines with names that look like ModuleNames also come to grief eventually.
"


Well, try this:

#!/usr/bin/perl -w use strict; use CGI; my $query=new CGI; sub new(*) { print "Ouch!!\n"} my $query2=new CGI;

What is this, another book bug? or its a perl problem, because if i dont use a typeglob on the prototype it works ...

$anarion=\$anarion;

s==q^QBY_^=,$_^=$[x7,print


Replies are listed 'Best First'.
Re: indirect object in camel book
by chromatic (Archbishop) on Oct 05, 2001 at 21:21 UTC
    What's happening is that the prototype tells Perl that main::new() accepts anything that might conceivably be a filehandle. The string 'CGI' certainly applies. Apparently this kind of thing is resolved before indirect method calls. (Makes sense to me, based on the locality principle.) Try, for example:

    my $query3 = new STDIN;

    Should that be a warning? I dunno. Is it a bug? I'm not sure. Seems like a good reason to avoid indirect object notation though, as well as prototypes. :)

      Worse yet, it looks as if almost anything might conceivably be a file handle, like all of the following -->

      #!d:/perl/bin/perl -w use strict; use CGI; my $query=new CGI; sub new(*) { print "Ouch!!\n"} my $query2=new CGI; my $query3 = new STDIN; my $query4 = new $query; my $query5 = new query; my $query6 = new 1; my $query7 = new ""; my $query8 = new '';

      This also works -- (prints Ouch)

      my $query9 = new @;

      although the next line is not interpreted correctly.

      -- Anthony Staines
Re: indirect object in camel book
by blakem (Monsignor) on Oct 06, 2001 at 00:27 UTC
Re: indirect object in camel book
by John M. Dlugosz (Monsignor) on Oct 06, 2001 at 02:13 UTC
    To prevent ambiguity, write:
    new CGI::;
    with the trailing :: after the module name.
      Nonononono. This is one reason why the indirect syntax is bad. You want to use the new function provided by the CGI package, right? Then, instead of forcing Perl to do your thinking for you, think for yourself!
      my $cgi = CGI->new;
      Tell Perl which new function you want to use!

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        I don't see your point. Saying new CGI:: is exactly the same in meaning to CGI->new. That is, you are telling Perl to look at a package named CGI and find the new function using virtual lookup rules.

        Using the ::, the point of my post, makes this syntax unambiguous.

Re: indirect object in camel book
by Anarion (Hermit) on Oct 06, 2001 at 04:46 UTC
    What anybody noticed of my post is the last line:
    "if i dont use a typeglob on the prototype it works"
    This calls the method of CGI
    #!/usr/bin/perl -w use strict; use CGI; my $query=new CGI; sub new($) { print "Ouch!!\n"} my $query2=new CGI;

    Then, I dont understand how is called a function or another depending of the definition of the prototype.

    $anarion=\$anarion;

    s==q^QBY_^=,$_^=$[x7,print


      It's because * in a prototype is used for a filehandle. Quick, what does this do:
      open MYCLASS, "$filename"; print MYCLASS; #print to MYCLASS or call MYCLASS->print?
      How about this?
      sub new(*) {...} open MYCLASS, $filename; new MYCLASS; #main::new(*MYCLASS) or MYCLASS->new?
      And this?
      sub new(*) {...} open CGI, $filename; new CGI; #main::new(*CGI) or CGI->new?
      In all those (except the first) it calls main::new, passing it the filehandle. And since filehandles autovivify, you don't even need to have it open()ed.

      =cut
      --Brent Dax
      There is no sig.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://117035]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2024-03-29 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found