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

How can I pass a reference to an anonymous empty array to a subroutine? Here is how I can pass an anonymous empty array to a subroutine:


&foo(qw());

Now how can I do the same with a reference to an anonymous empty array? I tried this:


&foo(\qw());

but that didn't work.

Replies are listed 'Best First'.
Re: passing anonymous empty array reference to subroutine
by choroba (Cardinal) on Apr 02, 2015 at 17:05 UTC
    Use square brackets:
    foo([]);

    See References for details.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: passing anonymous empty array reference to subroutine
by AnomalousMonk (Archbishop) on Apr 02, 2015 at 17:21 UTC
    ... pass an anonymous empty array to a subroutine:

    &foo(qw());

    A bit OT: I would say a better (because simpler) way to do this would be:
        foo();
    What would be the purpose of the empty  qw() call?

    BTW: I would also advise against using  & sigils for function calls in "modern" (i.e., post-4.xx version) Perl code; they will usually do no harm, but may introduce subtle bugs in certain cases (usually involving Prototypes — but let's just not involve prototypes!).


    Give a man a fish:  <%-(-(-(-<

Re: passing anonymous empty array reference to subroutine
by vinoth.ree (Monsignor) on Apr 02, 2015 at 20:01 UTC
    Hi, Here is how I can pass an anonymous empty array to a subroutine: &foo(qw());

    Here you are not passing anything. So obviously in function foo() @_ will be empty, it does not mean you are passing anonymous empty array.

    If you think you are passing anonymous empty array, it should be a first element in @_array, print as $_[0] you will get 'undef', as suggested by choroba use square bracket [] to pass empty anonymous array, then print $_[0] in foo() you will get a refernce to a anonymouse array as []


    All is well. I learn by answering your questions...
Re: passing anonymous empty array reference to subroutine
by Anonymous Monk on Apr 06, 2015 at 12:27 UTC
    How about, within the function, if the parameter-value received is undef, replace it with an empty-array ref and proceed?