cosmicperl has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,
  I have a few sub routines that take a list of data including refences to hashs and arrays. This is great when the hashes and arrays are variable, but sometimes they are short constants. I'd like to be able to put them in one line so that:-
my @array = (item1,item2...); my %hash = (key=value...); &mysubroutine(\@array,\%hash);
Becomes something like (this example doesn't work hence my asking):-
&mysubroutine(\(item1,item2),\(key=value));

Thanks in advance

Replies are listed 'Best First'.
Re: Passing or reference to a subroutine
by duff (Parson) on Dec 06, 2005 at 05:50 UTC

    Use [] to create a reference to an anonymous array and {} to create a reference to an anonymous hash. See perldoc perlreftut.

    some_sub(['foo','bar',baz'], { key1 => 1, key2 => 7 });

    P.S. Why are you putting &'s on your subroutines? That's slightly odd (even though, as I understand it, the Llama recommends the practice of always using the &)

Re: Passing or reference to a subroutine
by davido (Cardinal) on Dec 06, 2005 at 05:53 UTC

    You can do it like this:

    mysub( [ 'item1', 'item2' ], { key1 => 1, key2 => 2 } );

    The [] brackets create a reference to an anonymous literal array. And {} brackets create a reference to an anonymous literal hash.

    The way you were trying to do it doesn't work because \(item1, item2) is the same as ( \item1, \item2 )


    Dave

      For some reason I was putting a \ in front. God knows why. Thanks for the help.
Re: Passing or reference to a subroutine
by vennirajan (Friar) on Dec 06, 2005 at 05:51 UTC
    Can you explain your requirement more briefly ?