in reply to (jeffa) Re: Arrays
in thread How do I reset an array?

my @array = &getarray();

I tried it out and see that this works, but it doesn't seem to me that it should, since you appear to be storing a reference to the function in the array rather than the list of values returned by the function. Is this just a technique for deferring execution of getarray until the foreach my $val (@array) or is something else going on here?

Replies are listed 'Best First'.
(jeffa) 3Re: Arrays
by jeffa (Bishop) on May 06, 2002 at 20:06 UTC
    No deferrence going on here - &get_array() simple returns an array and not a reference (sub or array). Maybe this code will shed some light:
    use strict; use Data::Dumper; print Dumper get_array(); print Dumper get_array_ref(); print Dumper scalar get_something(); print Dumper get_something(); sub get_array { return ('a'..'d'); } sub get_array_ref { return [('a'..'d')]; } sub get_something { return wantarray ? ('a'..'d') : 'scalar'; }
    Also check out the difference between a list and an array

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      thanks, i appreciate all your help :)
      Im going to use all that info right now
      ^jasper <jasper@wintermarket.org>
      I get (no pun intended) how the original get_array works. What I'm unclear on is why you called it with &get_array() instead of just get_array(). I'm used to seeing \& used when setting up references to subs, but this is the first time I've encountered &subname() without the backslash.
        Oh! /smack forehead :P

        I only did that because that's how Jaspersan called the function. Personally, i hate prefixing function calls with the ampersand unless i am taking a reference to them, as you can probably tell from the rest of the code i posted.

        However, tye has chastised me for this, and i understand why - because it distinguishes your functions from Perl's internal functions. Parens are not enough:

        print "Hello World!\n"; print("Hello World!\n"); &print("Hello World?\n"); sub print { print "yo!\n"; }

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)
        
Re: Re: (jeffa) Re: Arrays
by c-era (Curate) on May 06, 2002 at 19:59 UTC
    That line just calls the sub and puts whatever is returned into @array. If you wanted to store a reference to a sub you would do this:
    my $subref = \&getarray;