This is one of those "I feel stupid for asking" questions because I'm sure the answer is obvious. I suspect it's the combined pressure of both
American Idol and 24 which is causing this blockage. But I digress...
I have a subroutine. I want it to be able to take either an array, or a reference to an array. The subroutine itself does its work on an array, so the question becomes one about how to best stuff the input to a subroutine into an array, regardless of whether the input is an array, or a reference to an array.
Here's what I've got so far:
sub thinger{
return unless $_[0];
my @array;
if((scalar @_) == 1){
return $_[0] unless ref($_[0]);
@array = @{$_[0]};
}
else{
@array = @_;
}
# Do something important
sleep 30000;
return wantarray ? @array : \@array;
}
Is that how it's done? Or is there some better way to determine whether I'm getting an array or an arrayref passed to the routine? Keep in mind I'm interested in how to get @array populated, not what is done with it or how it's returned.
Thanks
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.