Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > A doubt in using Class::Struct

Reply
Thread Tools

A doubt in using Class::Struct

 
 
None
Guest
Posts: n/a
 
      10-05-2006
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.

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      10-05-2006
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

 
Reply With Quote
 
 
 
 
None
Guest
Posts: n/a
 
      10-05-2006


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

 
Reply With Quote
 
None
Guest
Posts: n/a
 
      10-05-2006


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

 
Reply With Quote
 
None
Guest
Posts: n/a
 
      10-05-2006


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

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      10-05-2006
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

 
Reply With Quote
 
None
Guest
Posts: n/a
 
      10-05-2006

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

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      10-05-2006
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.

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...lass/Struct.pm

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

 
Reply With Quote
 
None
Guest
Posts: n/a
 
      10-05-2006
Thanks,

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

-Avi.

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.
>
> 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...lass/Struct.pm
>
> Sorry I can't be of more help . . .
> Paul Lalli


 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      10-05-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dotnet doubt can any body clarify my doubt challa462@gmail.com ASP .Net 0 08-22-2012 06:02 AM
ant doubt using war task harryos Java 0 12-03-2008 08:15 AM
doubt about doubt Bob Nelson C Programming 11 07-30-2006 08:17 PM
Doubt regarding C++ using STL ( VERYYY URGENT!!!! PLEASE HELP ) edu.mvk C++ 5 07-08-2006 08:01 AM
doubt in USING POINTERS ambika C Programming 134 10-04-2003 03:28 AM



Advertisments