in reply to How to store an array in the store of Bot::BasicBot::Pluggable::Module?

One bug is in line 22. It should be

my @order = ("orange","pea");

because '[]' creates a pointer to a constant array, not a constant array, the pointer gets generated in the next line

  • Comment on Re: How to store an array in the store of Bot::BasicBot::Pluggable::Module?
  • Download Code

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

    Thanks for your reply. I made that line up and you're right - it would lead to an array reference. Sadly though, this wasn't the problem. Both of the following snippets don't work, they both return 0:

    if ( $body =~ /^!seto\s*$/ ) { my @order = ("pea","plum","banana"); $self->set("fruit_order"=>@order); my @o = @{ $self->get("fruit_order") || [] }; return @o; } if ( $body =~ /^!seto\s*$/ ) { my @order = ("pea","plum","banana"); $self->set("fruit_order"=>\@order); my @o = @{ $self->get("fruit_order") || [] }; return @o; }

    As I digged a bit deeper into the according modules I couldn't find any example of an array being stored and retreived. Could it be possible that storing arrays is not supported by the 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');

        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!