Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

passing anonymous empty array reference to subroutine

by Special_K (Monk)
on Apr 02, 2015 at 17:03 UTC ( [id://1122282]=perlquestion: print w/replies, xml ) Need Help??

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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1122282]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-25 14:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found