Can't call method "blah" on unblessed reference at

Discussion in 'Perl Misc' started by Clint Olsen, Dec 18, 2003.

  1. Clint Olsen

    Clint Olsen Guest

    So, why would I get this diagnostic _if_ I never intended to call it in the
    method form? The subroutine in question is call like so:

    blah $foo;

    Where the subroutine foo is in the same package scope and it is called from
    a method, but $foo is _not_ the object. There isn't a stinkin' arrow
    there, so I don't know why Perl things I'm trying to call a method.

    If I add parentheses like so:

    blah($foo)

    Then things seem to progress.

    Confused,

    -Clint
     
    Clint Olsen, Dec 18, 2003
    #1
    1. Advertising

  2. Clint Olsen

    Uri Guttman Guest

    >>>>> "CO" == Clint Olsen <> writes:

    > blah $foo;


    > If I add parentheses like so:


    > blah($foo)


    > Then things seem to progress.


    from perlsyn:

    Declaring a subroutine allows a subroutine name to be used
    as if it were a list operator from that point forward in the
    program. You can declare a subroutine without defining it
    by saying "sub name", thus:

    sub myname;
    $me = myname $0 or die "can't get myname";

    you can't just use a sub as a list operator without predeclaring it. you
    can put the sub's code before its use and that will work also. perl has
    no way to differentiate these:

    foo $blah # sub call
    Foo $arg # indirect object call (class method).

    so unless it knows foo is a sub it chooses the latter and in your case
    barfs up the error.

    the parens always mark it as a sub (no possible ambiguity) so it works
    fine. this is a good reason to always use parens for your sub calls
    (unless you want them to look like operators and then you have to
    declare them in advance).

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Dec 18, 2003
    #2
    1. Advertising

  3. Clint Olsen <> writes:

    > So, why would I get this diagnostic _if_ I never intended to call it in the
    > method form? The subroutine in question is call like so:
    >
    > blah $foo;
    >
    > Where the subroutine foo is in the same package scope and it is called from
    > a method, but $foo is _not_ the object. There isn't a stinkin' arrow
    > there, so I don't know why Perl things I'm trying to call a method.


    If you look up "Method Invocation" in the relevant manual (perlobj)
    you'll find that the very first thing it say is "There are two ways to
    invoke a method...".

    > If I add parentheses like so:
    >
    > blah($foo)
    >
    > Then things seem to progress.


    That is correct. You can call a subroutine using the explicit
    parentheses syntax even if it's not yet been declared. You can't call
    a subroutine without parentheses until after the declaration.

    This is, IIRC, explained in "perlsub".

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
     
    Brian McCauley, Dec 18, 2003
    #3
  4. Clint Olsen () wrote:
    : So, why would I get this diagnostic _if_ I never intended to call it in the
    : method form? The subroutine in question is call like so:

    : blah $foo;

    : Where the subroutine foo is in the same package scope and it is called from
    : a method, but $foo is _not_ the object. There isn't a stinkin' arrow
    : there, so I don't know why Perl things I'm trying to call a method.

    : If I add parentheses like so:

    : blah($foo)

    : Then things seem to progress.

    : Confused,

    $foo->blah()

    is the same as

    blah $foo

    but the second form (so I am told) is ambiguous. Apparently, in your
    case, perl thought you were trying to use that second form. The reasons
    for that are in the rest of the code that you don't show - something about
    the syntax you used made perl think you wanted to call a method.

    The brackets dis-ambiguated this to be a simple function call.

    I suspect that predeclaring the function would also do that, but I don't
    that enough myself to say for sure.
     
    Malcolm Dew-Jones, Dec 18, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. evolve
    Replies:
    2
    Views:
    494
    William Ryan
    Nov 2, 2003
  2. A. Kong
    Replies:
    2
    Views:
    4,731
    Iwo Mergler
    Oct 11, 2004
  3. Hylander
    Replies:
    0
    Views:
    408
    Hylander
    Jan 16, 2004
  4. yaru22
    Replies:
    25
    Views:
    697
    Daniel Dyer
    Jan 8, 2007
  5. Gunnar Strand
    Replies:
    2
    Views:
    210
    Gunnar Strand
    Jun 16, 2004
Loading...

Share This Page