Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A doubt in using Class::Struct

1 view
Skip to first unread message

None

unread,
Oct 5, 2006, 2:34:11 PM10/5/06
to
Hello,

I am trying to use Class::Struct, for creating a structure as follows:

package Classes;
use Class::Struct;
struct(messages => '@');

$msg = new Classes;
push(@abc, "HELLO");
push(@abc, "WORLD");
push(@abc, "BYE");
$msg->messages(@abc);

@xyz = @{$msg->messages};
foreach $elem(@xyz)
{
print $elem ."\n";
}

This gives the following error on compilation:

Too many args to messages at (eval 1) line 17
Classes::messages('Classes=ARRAY(0x8103084)', 'HELLO', 'WORLD',
'BYE')

Also, if I remove the third element of the array, the program compiles,
but I get the following output:
WORLD

The element "HELLO is not displayed.
Can anyone help me with this or let me know what the problem is?

Thanks,
Avi.

Paul Lalli

unread,
Oct 5, 2006, 2:59:04 PM10/5/06
to
None wrote:
> I am trying to use Class::Struct, for creating a structure as follows:
>
> package Classes;

Where are
use strict;
use warnings;
?

I bet they would have given you some clues as to what you did wrong....

> use Class::Struct;
> struct(messages => '@');
>
> $msg = new Classes;
> push(@abc, "HELLO");
> push(@abc, "WORLD");
> push(@abc, "BYE");
> $msg->messages(@abc);

Please read the documentation for the module you're using!
>From perldoc Class::Struct:
=====================================
Array ('@' or '*@')
The element is an array, initialized by default to ().

With no argument, the accessor returns a reference to the element's
whole array (whether or not the element was specified as '@' or '*@').

With one or two arguments, the first argument is an index specifying
one element of the array; the second argument, if present, is assigned
to the array element. If the element type is '@', the accessor returns
the array element value. If the element type is '*@', a reference to
the array element is returned.

As a special case, when the accessor is called with an array reference
as the sole argument, this causes an assignment of the whole array
element. The object reference is returned.
======================================

If you want to set the entire array, you need to pass a reference to
the array, not the array itself.

Paul Lalli

None

unread,
Oct 5, 2006, 3:17:35 PM10/5/06
to

>
> If you want to set the entire array, you need to pass a reference to
> the array, not the array itself.
>

I tried doing that as follows:

package Classes;


use Class::Struct;
struct(messages => '@');

$msg = new Classes;
push(@abc, "HELLO");
push(@abc, "WORLD");
push(@abc, "BYE");

$msg->messages(\@abc);

@xyz = @{$msg->messages};
foreach $elem(@xyz)
{
print $elem ."\n";
}

But, even with this, the "print" statement does not print out anything!
I am a newbie to perl programming. So, please let me know if I am
overlooking some small error!

Refrence:
$msg->messages(\@abc);

Entire array:
$msg->messages(@abc);

In either case, I do not get the full array printed out! Thanks for the
help!

-Avi.

None

unread,
Oct 5, 2006, 3:21:05 PM10/5/06
to

>
> If you want to set the entire array, you need to pass a reference to
> the array, not the array itself.
>

I tried doing that as follows:

package Classes;


use Class::Struct;
struct(messages => '@');

$msg = new Classes;
push(@abc, "HELLO");
push(@abc, "WORLD");
push(@abc, "BYE");

$msg->messages(\@abc);

@xyz = @{$msg->messages};
foreach $elem(@xyz)
{
print $elem ."\n";
}

But, even with this, the "print" statement does not print out anything!

None

unread,
Oct 5, 2006, 3:21:48 PM10/5/06
to

>
> If you want to set the entire array, you need to pass a reference to
> the array, not the array itself.
>

I tried doing that as follows:

package Classes;


use Class::Struct;
struct(messages => '@');

$msg = new Classes;
push(@abc, "HELLO");
push(@abc, "WORLD");
push(@abc, "BYE");

$msg->messages(\@abc);

@xyz = @{$msg->messages};
foreach $elem(@xyz)
{
print $elem ."\n";
}

But, even with this, the "print" statement does not print out anything!

Paul Lalli

unread,
Oct 5, 2006, 3:28:01 PM10/5/06
to
None wrote:
> >
> > If you want to set the entire array, you need to pass a reference to
> > the array, not the array itself.
> >
>
> I tried doing that as follows:
>
> package Classes;
> use Class::Struct;
> struct(messages => '@');
>
> $msg = new Classes;
> push(@abc, "HELLO");
> push(@abc, "WORLD");
> push(@abc, "BYE");
> $msg->messages(\@abc);
>
> @xyz = @{$msg->messages};
> foreach $elem(@xyz)
> {
> print $elem ."\n";
> }
>
> But, even with this, the "print" statement does not print out anything!

Then you copy and pasted wrong. Because when I copy and paste the
above into a new file, and run that file, the output I get is:
HELLO
WORLD
BYE

Paul Lalli

None

unread,
Oct 5, 2006, 3:38:15 PM10/5/06
to

> Then you copy and pasted wrong. Because when I copy and paste the
> above into a new file, and run that file, the output I get is:
> HELLO
> WORLD
> BYE


I copied and pasted the same code in a new file as well, but I am not
getting any output from the execution. I am not able to get if there
any problem with the version of perl, or a problem with the program!

perl --version:
This is perl, v5.6.1 built for i686-linux....


Thanks,
Avi.

Paul Lalli

unread,
Oct 5, 2006, 3:52:10 PM10/5/06
to

Interesting. I get no output with Perl v.5.6.1 as well, using module
version 0.59. However, with Perl v5.8.4 and Class::Struct version
0.63, I get the desired output.

My first and obvious recommendation is that you upgrade. If that's not
feasable for you, consult the documentation for the version of
Class::Struct on your system, and see if there are any differences
between that and the documentation found at:
http://search.cpan.org/~nwclark/perl-5.8.8/lib/Class/Struct.pm

Sorry I can't be of more help . . .
Paul Lalli

None

unread,
Oct 5, 2006, 3:57:01 PM10/5/06
to
Thanks,

I tried inserting into the array using indices and that works... thanks
for the help!

-Avi.

Paul Lalli

unread,
Oct 5, 2006, 3:58:01 PM10/5/06
to
Paul Lalli wrote:
> None wrote:
> > > Then you copy and pasted wrong. Because when I copy and paste the
> > > above into a new file, and run that file, the output I get is:
> > > HELLO
> > > WORLD
> > > BYE
> >
> >
> > I copied and pasted the same code in a new file as well, but I am not
> > getting any output from the execution. I am not able to get if there
> > any problem with the version of perl, or a problem with the program!
> >
> > perl --version:
> > This is perl, v5.6.1 built for i686-linux....
>
> Interesting. I get no output with Perl v.5.6.1 as well, using module
> version 0.59. However, with Perl v5.8.4 and Class::Struct version
> 0.63, I get the desired output.
>
> Sorry I can't be of more help . . .

Actually, it turns out I can be of more help. I did a diff of the two
.pm files on my system, and found that the special case of calling the
accessor with a reference to an array is new. In version 0.59, it
seems the only way to set an array of values is to set them one at a
time:
for my $i (0..$#abc){
$msg->messages($i, $abc[$i]);
}

The above works for me with both versions of the module.

Paul Lalli

Brian Helterline

unread,
Oct 5, 2006, 4:00:07 PM10/5/06
to
None wrote:
> Hello,
>
> I am trying to use Class::Struct, for creating a structure as follows:
>
> package Classes;
> use Class::Struct;
> struct(messages => '@');
>
> $msg = new Classes;
> push(@abc, "HELLO");
> push(@abc, "WORLD");
> push(@abc, "BYE");
> $msg->messages(@abc);

according to the docs, array type structs take 2 elements, the index and
the value so you would populate it like this:

$msg->messages(0, 'HELLO');
$msg->messages(1, 'WORLD');
$msg->messages(2, 'BYE');

>
> @xyz = @{$msg->messages};
> foreach $elem(@xyz)
> {
> print $elem ."\n";
> }
>
> This gives the following error on compilation:
>
> Too many args to messages at (eval 1) line 17
> Classes::messages('Classes=ARRAY(0x8103084)', 'HELLO', 'WORLD',
> 'BYE')
>
> Also, if I remove the third element of the array, the program compiles,
> but I get the following output:
> WORLD

with two elements, the first is interpeted as an index ("HELLO" => 0)
and the value stored there is 'WORLD'

0 new messages