perloopperl-data-structures

Isn't this an array reference? Why isn't it an array reference?


I'm trying to debug a puzzling problem with the following lines, deep in the guts of a program I've inherited,

my $log = [];
$root->validate_subtree($log); 

results in the following error

Treex::PML::Node::validate: log must be an ARRAY reference

$root is an object of type Treex::PML::Node, and and the two lines really are adjacent-- I haven't left anything out. I went to the source of the error message, andvalidate_subtree() checks its argument as follows (Node.pm line 423):

sub validate_subtree {
  my ($node, $log) = @_;
  if (defined $log and UNIVERSAL::isa($log,'ARRAY')) {
    croak __PACKAGE__."::validate: log must be an ARRAY reference";
  }

I thought that $log = [] does initialize $log to an array reference! What am I missing?


Solution

  • You are correct. The code you've shown does set $log to an array reference. This code:

    sub validate_subtree {
      my ($node, $log) = @_;
      if (defined $log and UNIVERSAL::isa($log,'ARRAY')) {
        croak __PACKAGE__."::validate: log must be an ARRAY reference";
      }
    

    croaks precisely when $log is in fact an array reference. I would guess you've found a bug in the distribution, and it should be

      if (defined $log and not UNIVERSAL::isa($log,'ARRAY')) {