water has asked for the wisdom of the Perl Monks concerning the following question:

How does one push a new value onto an array slot in an object set up with Class::MakeMethods?

Anything more elegant than copying out then back in?

# untested my @x = $object->x_array(); push(@x, $new_value); $object->x_array(@x)
Thanks!

water

Replies are listed 'Best First'.
Re: implementing 'push' in Class:MakeMethods
by Errto (Vicar) on Apr 28, 2005 at 03:14 UTC
    I haven't used Class::MakeMethods, but the normal way to push something onto an array returned by some function is
    push @{$object->somemethod()}, $new_value;
    But this requires somemethod to return an array reference, not a list. If x_array does that, you're all set.
Re: implementing 'push' in Class:MakeMethods
by YuckFoo (Abbot) on Apr 28, 2005 at 20:20 UTC
    When you set up the slot as type 'list', you will have all the list methods.

    ListFoo

    #!/usr/bin/perl use strict; my $stuff = Todo::List->new(); $stuff->todo_push('I really need to read the docs.'); $stuff->todo_push('I will use the perldoc command.'); $stuff->todo_push('perldoc Class::MethodMaker'); while (my $todo = $stuff->todo_shift()) { print "$todo\n"; } #----------------------------------------------------------- package Todo::List; use strict; use Class::MethodMaker new_hash_init => [qw(new)], list => [qw(todo)];
      errrrr....  Class::MakeMethods, not  Class::MethodMaker.

      The names are realy similar, but the modules aren't.

      ?

Re: implementing 'push' in Class:MakeMethods
by Joost (Canon) on Apr 28, 2005 at 11:27 UTC