in reply to Memcached and arrays?

If you're having a problem, stop hiding the diagnostics. Use use strict; use warnings;! They wouldn't have helped here (except to detect unrelated issues), but you didn't know that.

I love how you repeat that it works for arrays and it doesn't work for arrays. The problem is surely in the question.

Your code is equally fishy.

$memd->set("mem_id_cache", \@array); ^^^^^^^ scalar @tmp = $memd->get("mem_id_cache"); ^^^^ array

Why do you expect to get something different than you put in?

Storing @array yeilds nothing.

I'm curious as to how you tested that since functions can only take scalars as arguments.

Passing the contents of the array is obviously wrong too, since the documentation that says you must supply *a* scalar for the value.

Since you can't change what you store, you need to change what you expect to fetch.

my @items_to_stored = qw( a b c ); $memd->set("mem_id_cache", \@items_to_stored); my $stored_items = $memd->get("mem_id_cache"); print "$_\n" for @$stored_items;

Replies are listed 'Best First'.
Re^2: Memcached and arrays?
by BrowserUk (Patriarch) on Jan 21, 2010 at 05:59 UTC
    my @items_to_stored = qw( a b c ); $memd->set("mem_id_cache", \@items_to_stored); my $stored_items = $memd->get("mem_id_cache"); print "$_\n" for @$stored_items;

    Surely, that would only work, if at all, if you retrieve the array ref in the same process as you stored it. Otherwise the reference would just point to some random chunk of (probably unallocated) memory.

    And what is the point of storing a ref in a cache then retreiving it, when you have direct access to the data it points to?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      No, it serialises and deserialises data structures for you (using Storable::freeze and Storable::thaw by default). $stored_items holds a reference to a copy of the original array.

      This was a simple test if I could actually store results in Memcached then retrieve them at a later time. Success!

        Ignore!

        The point is that if you only store an array reference, and then retreive that reference in the same process, then the reference will still point to the array, and you'll be able to access its contents through it.

        But if you stored the reference in one process, and then retrieved it in another, then that reference will not point to anything in the new process and if you attempt to dereference it, you'll probably crash perl.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: Memcached and arrays?
by expresspotato (Beadle) on Jan 21, 2010 at 06:03 UTC
    I thought subroutines can accept arrays then passed to @_? The plan was to use this code segment once working in a larger context.
    sub mem_get{ my $tmp = $memd->get($_[0]); if (ref($tmp) eq "ARRAY"){ return @{$tmp}; }else{ return $tmp; }

      I thought subroutines can accept arrays then passed to @_

      That sentence is a contradiction. Array element can only be scalars, so @_ can't contain an array. It can only contain a reference to one since a reference is a scalar.

      In

      foo(@a)

      @a evaluates to its content, and those are used as the arguments. The above is the same as

      foo($a[0], $a[1], $a[2], $a[3]);

      The plan was to use this code segment once working in a larger context.

      Careful

      my $old_x = [qw( a b c )]; $memd->set(x => $x); my $new_x = mem_get('x'); # $new_x = 3 !?