A doubt in using Class::Struct

N

None

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.
 
P

Paul Lalli

None said:
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
 
N

None

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.
 
N

None

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.
 
N

None

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.
 
P

Paul Lalli

None said:
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
 
N

None

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.
 
P

Paul Lalli

None said:
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.

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
 
N

None

Thanks,

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

-Avi.
 
P

Paul Lalli

Paul said:
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
 
B

Brian Helterline

None said:
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'
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top