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

Chapter 6 of Camel3 says:
foo; # like foo() if sub foo predeclared, else bareword "foo"
One of my scripts is similar to the following:
use CGI; my $q = new CGI; print $q->p "hello world";
To my surprise the above code snipped fails. It also fails if I replace 'use' with 'require'. But if I surround the arguments to p with parentheses then it works, i.e.,
use CGI; my $q = new CGI; print $q->p("hello world");
If for whatever reasons one wanted to predeclare a member function of a CGI object how would one do that? Clearly, perl does not consider 'use' or 'require' to constitute a predeclaration.

Replies are listed 'Best First'.
(tye)Re: Can One Predeclare a Package's Methods?
by tye (Sage) on May 25, 2001 at 23:41 UTC

    You've managed to sidetrack most people with the mention of "predeclare". What you really want to know is "Can you pass arguments to a method without using parens?" The answer is "no".

    At least part of the reason that the answer is "no" is that, in Perl5, methods are often used as accessors for member variables so it is convenient to write:

    $width= $obj->width; $width= $obj->right - $obj->left;
    without needing parens. So we can't make that last line start parsing as: $width= $obj->right( - $obj->left ); But you'd like to have that as an option. Well, we can consider making that option apply to a scope (like use strict), to a class, to an object, or to a method. But this option has to be determined at compile time otherwise we wouldn't be able to compile our example line. And Perl does late binding so we can't determine the class, object, nor method at compile time (we can often determine the method name at compile time but we can't determine what class it belongs to and making all methods named "size" not require parens no matter what class they are for just doesn't make much sense). So the only possibility would be to make this option apply to a given scope.

    So now you can start working on that patch to add a new "compiler hint" bit to implement this. (:

            - tye (but my friends call me "Tye")
Re: Can One Predeclare a Package's Methods?
by converter (Priest) on May 25, 2001 at 22:22 UTC
    Predeclaration and prototypes don't work for methods. See the Prototypes section of the perlsub manpage.

    From perlsub:

    Method calls are not influenced by prototypes either, because the function to be called is indeterminate at compile time, since the exact code called depends on inheritance.
Re: Can One Predeclare a Package's Methods?
by stephen (Priest) on May 25, 2001 at 23:25 UTC
    If you simply want to use p et al without parens, you can simply do:
    use CGI qw(p); print p 'hello';
    which yields
    <P>hello</P>
    That still doesn't give us predeclared methods, for the reasons mentioned by others, but it'll work for the specific case. (use CGI qw(:standard) keeps you from having to import everything individually.)

    Personally, I'd still use the parens for clarity, since my above call looks too much like I'm trying to print "hello" to a filehandle named "p".

    stephen

Re: Can One Predeclare a Package's Methods?
by bikeNomad (Priest) on May 25, 2001 at 22:26 UTC
    You can declare something in another package using the form:
    sub CGI::p;

    But notice that CGI::p in this case is not actually a subroutine; it is implemented using AUTOLOAD.

    And, as noted above, it's impossible to pre-declare a method call because it's subject to inheritance (for this same reason, prototypes don't work for methods).