Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^7: How to combine these 2 subroutines in one?

by Anonymous Monk
on Apr 04, 2014 at 09:14 UTC ( #1081092=note: print w/ replies, xml ) Need Help??


in reply to Re^6: How to combine these 2 subroutines in one?
in thread How to combine these 2 subroutines in one?

I see!
So please tell me if I am correct, if the list has been passed as an array, then $type_of_arg will be empty, whereas if it's passed as a reference to an array it's ARRAY, right?


Comment on Re^7: How to combine these 2 subroutines in one?
Download Code
Re^8: How to combine these 2 subroutines in one?
by Athanasius (Monsignor) on Apr 04, 2014 at 09:36 UTC

    Correct!

    That is, provided that the passed-in-array does not itself contain references. For the purpose of this exercise, you may know that it won’t, but in the general case, it might, which is why a solution using wantarray may be preferable here, as explained above.

    Note that “empty” here means the empty string, i.e. "", so to make the test more robust you could write:

    my $type_of_arg = ref $_[0]; if ($type_of_arg eq "ARRAY") { # Handle an array reference } elsif ($type_of_arg eq "") { # Handle an array } else { die "Unexpected reference type '$type_of_arg': $!"; }

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thank you so much for these tips, references have always been a headache to me... Actually, I don't quite understand what do we use them for, I need to study up on this more!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1081092]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2014-08-21 21:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (143 votes), past polls