Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

It is not an array!

by hardburn (Abbot)
on Nov 18, 2002 at 04:34 UTC ( [id://213654]=perlquestion: print w/replies, xml ) Need Help??

hardburn has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing an object that has a fairly generic beginning:

package Module::Name; use strict; BEGIN { use Exporter (); use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); $VERSION = 0.01; @ISA = qw (Exporter); @EXPORT = qw (); @EXPORT_OK = qw (bunch_of_methods_and_constant_vars); %EXPORT_TAGS = ( constants => [qw(constant_vars)], ); }

Nothing earth-shattering here. I run this as the initial test:

# -*- perl -*- # t/001_load.t - check module loading and create testing directory use Test::More tests => 2; BEGIN { use_ok(Module::Name qw(:constants)); } my $object = Module::Name->new(); isa_ok ($object, 'Module::Name');

Again, nothing surprising. This is all generated automatically from ExtUtils::MakeMaker. However, I get this output from the 'make test':

t/001_load....NOK 2# Failed test (t/001_load.t at line 12) # The object isn't a 'Module::Name' it's a 'ARRAY'

Why does it think it's an array?

Replies are listed 'Best First'.
Re: It is not an array!
by sauoq (Abbot) on Nov 18, 2002 at 05:14 UTC

    I don't think this has anything to do with blessing your object as you get the error when you attempt to use the module. I think you just forgot a comma...

    use_ok(Module::Name qw(:constants));
    should be use_ok(Module::Name, qw(:constants));

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: It is not an array!
by Zaxo (Archbishop) on Nov 18, 2002 at 04:47 UTC

    I don't see where you've defined sub new for the class. Here's a modest general-purpose one:

    # in the Module::Name package sub new { my $class = shift; bless {@_}, $class; }
    The object is a hashref blessed into the package which calls it. It's initialized with a hashlike list, same as an ordinary %hash.

    After Compline,
    Zaxo

Re: It is not an array!
by pg (Canon) on Nov 18, 2002 at 04:58 UTC
    Guess you didn't bless it, and you used array as the base for your class.
    This is easy to check. If you do a print of that object, and it is a class, you would see 'class_name=ARRAY(0xnnnnnnnn), in stead of ARRAY(0xnnnnnnnn). If you use hash as base, it would use the word HASH to replace ARRAY.

Re: It is not an array!
by premchai21 (Curate) on Nov 18, 2002 at 04:42 UTC
    My guess is that the object might not be being blessed properly, but without being able to see the Module::Name::new function, there's not much I (we?) can tell about it. Could you post it please?

      This was the constructor:

      sub new { my ($class, %parameters) = \@_; my $self = bless ({}, ref ($class) || $class); return ($self); }

      This came right out of ExtUtils::ModuleMaker. After reading some of the comments in this node, I flipped through the Camel and found this suggestion:

      sub new { my $invocant = shift; my $class = ref($invocant) || $invocant; my $self = { }; bless($self, $class); return $self; }

      Which appears to me to do basically the same thing (except it's not taking a hashref as a param, which shouldn't matter, and I don't need it anyway). However, the tests all pass with this version.

        I see what is going on now. This line:
        my ($class, %parameters) = \@_;
        makes a reference to the array @_, and assigns $class that reference. %parameters is left blank. Calling ref on an unblessed array reference returns ARRAY; the next line then blesses $self as ARRAY. Removing the backslash in the first line would cause $class to get the first argument, and %parameters to get the other arguments converted into a hash, which seems to be more what you want.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://213654]
Approved by premchai21
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (2)
As of 2025-03-18 03:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (56 votes). Check out past polls.