Search code examples
perlmethodscoderef

Why does Perl allow invoking coderefs on unblessed data structures?


When executing the statement $obj->method();, perldiag says that Perl needs to know what package the method belongs to. That's why it needs to be blessed:

Can't call method "%s" on unblessed reference

(F) A method call must know in what package it's supposed to run. It ordinarily finds this out from the object reference you supply, but you didn't supply an object reference in this case. A reference isn't an object reference until it has been blessed. See perlobj.

Because of this, it isn't possible to do the following:

my $data = [
             [ 1, 2, 3 ],
             [ 4, 5, 6 ],
           ];

$data->process( @params );  # Can't call method "process" on unblessed reference

Then why does it work with a coderef?:

my $process = \&process;    # Same method as before
$data->$process( @params ); # Works fine now

Solution

  • $variable->method(@args)
    

    just invokes

    method($variable, @args)
    

    But in which package should Perl find the method subroutine? If $variable is a blessed reference, Perl will look for the subroutine in the package returned by ref $variable. If $variable is a string and the name of a package, Perl will look for the subroutine in that package name.

    In your second example, when you declare

    $process = \&process
    

    you have given Perl the reference to the code you want to invoke, so Perl knows to take the call

    $variable->$process(@args)
    

    and invoke

    $process->($variable, @args)
    

    or

    &process($variable, @args)
    

    It is only when $variable is an unblessed reference, and the method name can not be resolved into a code reference, that Perl cannot figure out what to do and punts.