access object

L

Lin Jingxian

Hi,
I have generate some objects in one subroutine, now I pass the object array
to another subroutine, how can I access the objects.

for example:

use Class::Struct;

struct Field => {
name=>'$',
type=>'$',
};

my $field = \Field->new();
$field->name("username");
$field->type("varchar");

my @fields;
push @fields,$field;

show_field(@field);

sub show_field{
my @fields = @_;

my $field = shift @fields;
print $field->name(); ########## here I got : Can't call method
"name" on unblessed reference
print $field->type();


};

Perl don't think $field as a object, what should I do ?

thanks.
 
J

Jay Tilton

: I have generate some objects in one subroutine, now I pass the object array
: to another subroutine, how can I access the objects.
:
: for example:
:
: use Class::Struct;
:
: struct Field => {
: name=>'$',
: type=>'$',
: };
:
: my $field = \Field->new();
^
Lose that backslash.

: $field->name("username");
: $field->type("varchar");
:
: my @fields;
: push @fields,$field;
:
: show_field(@field);
^^^^^^
Your array is named @fields, not @field.
"use strict;" would have caught that.

: sub show_field{
: my @fields = @_;
:
: my $field = shift @fields;
: print $field->name(); ########## here I got : Can't call method
: "name" on unblessed reference

When posting code, be aware of how your news client handles line-wrapping.

: print $field->type();
: };
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top