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

C

Clint Olsen

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
 
U

Uri Guttman

CO" == Clint Olsen said:
blah $foo;
If I add parentheses like so:

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
 
B

Brian McCauley

Clint Olsen said:
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\\
 
M

Malcolm Dew-Jones

Clint Olsen ([email protected]) 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top