A doubt in using Class::Struct

Discussion in 'Perl Misc' started by None, Oct 5, 2006.

  1. None

    None Guest

    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.
    None, Oct 5, 2006
    #1
    1. Advertising

  2. None

    Paul Lalli Guest

    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
    Paul Lalli, Oct 5, 2006
    #2
    1. Advertising

  3. None

    None Guest


    >
    > 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, Oct 5, 2006
    #3
  4. None

    None Guest


    >
    > 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, Oct 5, 2006
    #4
  5. None

    None Guest


    >
    > 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, Oct 5, 2006
    #5
  6. None

    Paul Lalli Guest

    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
    Paul Lalli, Oct 5, 2006
    #6
  7. None

    None Guest


    > 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.
    None, Oct 5, 2006
    #7
  8. None

    Paul Lalli Guest

    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-5.8.8/lib/Class/Struct.pm

    Sorry I can't be of more help . . .
    Paul Lalli
    Paul Lalli, Oct 5, 2006
    #8
  9. None

    None Guest

    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-5.8.8/lib/Class/Struct.pm
    >
    > Sorry I can't be of more help . . .
    > Paul Lalli
    None, Oct 5, 2006
    #9
  10. None

    Paul Lalli Guest

    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
    Paul Lalli, Oct 5, 2006
    #10
  11. 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'

    >
    > The element "HELLO is not displayed.
    > Can anyone help me with this or let me know what the problem is?
    >
    > Thanks,
    > Avi.
    >
    Brian Helterline, Oct 5, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Fogelklou
    Replies:
    36
    Views:
    1,353
    Chris Fogelklou
    Apr 20, 2004
  2. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    595
  3. Replies:
    0
    Views:
    538
  4. Peter Otten
    Replies:
    2
    Views:
    103
    Cousin Stanley
    Aug 10, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    106
    Terry Reedy
    Aug 10, 2013
Loading...

Share This Page