in reply to Re^2: How to store an array in the store of Bot::BasicBot::Pluggable::Module?
in thread How to store an array in the store of Bot::BasicBot::Pluggable::Module?

If at all, the second of your two snippets should work. And it should even work if storing arrays isn't possible because a reference behaves like a scalar in most situations. For example if you print it, it says "ARRAY(0x...)".

I notice that you write in your first post "Setting a scalar value (is_weekend) is straight from the documentation and should work". You might test that first, even documentation can have bugs. This should help get some answers:

# instead of my @o = @{ $self->get("fruit_order") || [] }; my $o= $self->get("fruit_order"); use Data::Dumper; print Dumper($o),"\n"; print ref $o,"\n"; return @$o if (ref $o eq 'ARRAY');
  • Comment on Re^3: How to store an array in the store of Bot::BasicBot::Pluggable::Module?
  • Download Code

Replies are listed 'Best First'.
Re^4: How to store an array in the store of Bot::BasicBot::Pluggable::Module?
by brengo (Acolyte) on Jan 22, 2011 at 17:21 UTC

    Boy, was I stupid... Thank you! It works now with the following snippet:

    if ( $body =~ /^!setq\s*$/ ) { my @order = ("orange","peach","banana"); $self->set("fruit_order"=>\@order); my $o= $self->get("fruit_order"); my $ou= "order: ".join(", ",@$o); return $ou if (ref $o eq 'ARRAY'); return "no order :("; }

    Why it didn't work before, you ask? Well, if you look closely you'll find that I tried to use return with an array. It will not dump the contents of the array into the channel though but expects a scalar (so it sent a 0 for an empty array and a 3 for the above example). I had to use string concatenation and a helper variable first to get return to paste the contents of the array.

    Works now, thanks again!

      Returning an array works too, you just have to provide list context on the calling side, i.e.

      sub x { ... return @f; } my @r= x(...);

      would work. You don't need the concatenation

        I can't provide list context on the calling side though because the "return" of the function is defined/used by the Bot::BasicBot::Pluggable::Module class (or one of it's ancestors) to directly speak to the irc channel. Everything that gets returned should be said in the channel by the bot. Apparently the programmers of that module didn't think of a possible list context case.

        So it was one of my mistakes to assume that array elements (of a returned array) would be printed out (=said in the channel) automatically...