in reply to passing array ref without variable

Yes, just try it – and see what happens with Data::Dumper or any of its ilk. (Also nice to use strictures and warnings!)

>perl -wMstrict -le "use Data::Dumper; my %hash = qw(a 1 b 2 c 3 d 4); my_function([keys %hash]); sub my_function { my $arr_ref = shift; print Dumper $arr_ref; } " $VAR1 = [ 'c', 'a', 'b', 'd' ];

Replies are listed 'Best First'.
Re^2: passing array ref without variable
by vit (Friar) on May 03, 2010 at 15:23 UTC
    Can I pass somehow an array ref of values without separately forming an array? Maybe using foreach...
      Why would you want to? Inline anonymous array construction is a trivial number of characters and is robust. This sounds like an XY Problem.
      I suppose what you really want/need is to pass the hash as reference, like this you don't need to pass the values and the keys separately.

       call(\%hash)

      If you explicitly want to pass the reference of a copy do this

       call( { %hash } )

      the { ... } returns a ref of an anonymous hash of the list expanded from %hash, i.e. the ref of a hashcopy!

      Cheers Rolf

        No I do not want to pass a hash. What I want to do is having a hash ($h{key} = value) and
        sub my_function { my $arr_ref = shift; ... }
        I can pass keys as an array ref by
        my_function([keys %hash])
        Now, I want to pass values as an array ref in a shortest way without forming an array. Is it possible?

      Why would you want to? And how could you? It sounds like you (vit) are asking "Can I take a reference to an array that does not exist?".