Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Can't call method "blah" on unblessed reference at (http://www.velocityreviews.com/forums/t884271-cant-call-method-blah-on-unblessed-reference-at.html)

Clint Olsen 12-18-2003 06:25 PM

Can't call method "blah" on unblessed reference at
 
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

Uri Guttman 12-18-2003 06:38 PM

Re: Can't call method "blah" on unblessed reference at
 
>>>>> "CO" == Clint Olsen <clint@0lsen.net> 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 ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

Brian McCauley 12-18-2003 06:42 PM

Re: Can't call method "blah" on unblessed reference at
 
Clint Olsen <clint@0lsen.net> 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\\

Malcolm Dew-Jones 12-18-2003 07:03 PM

Re: Can't call method "blah" on unblessed reference at
 
Clint Olsen (clint@0lsen.net) 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.


All times are GMT. The time now is 11:53 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.