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

hello dear brothers !

I have a function that gets a reference to an array:
sub myfunc { my $arrRef = shift; # do my stuff here }
and I have a scalar that I wonna send this function. Is there a short way of doing that without having to define a new array variable as the followings:
my @arr = ($var); &myfunc(\@arr);
btw, I'v tried the followings and it doesn't work:
&myfunc(\($var));


Hotshot

Replies are listed 'Best First'.
(jeffa) Re: Reference on calling to a function
by jeffa (Bishop) on Dec 02, 2001 at 21:51 UTC
    How about:
    &myfunc([$var]);

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
      And what that does is create an anonymous array from the list between the brackets and return a reference to it. So:
      my @array = qw/1 2 3 4/; myfunc( \@array );
      and
      myfunc( [ qw/1 2 3 4/ ] );
      are functionally equivalent.

      [ ar0n -- want job (boston) ]

(podmaster) Re: Reference on calling to a function
by PodMaster (Abbot) on Dec 03, 2001 at 05:08 UTC
    What you say? You haven't (speculation) read perlref or perldata? Well you better go read it now, because it is where the knowledge you seek is contained.