Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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

by Anonymous Monk
on Apr 03, 2014 at 23:13 UTC ( #1081036=note: print w/replies, xml ) Need Help??

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

Ok, I tried this
sub clean_list { my $type_of_arg=ref(@{ $_[0] }); print "Type of argument passed is: $type_of_arg\n"; } open(IN, '<', 'ex5.acc') or die "Could not read file\n"; my @initial_array = <IN>; close IN; clean_list(@initial_array); clean_list(\@initial_array);

and, if I pass the list as a reference it works OK, whereas, if I pass it as a simple array I get:
Can't use string ("L13923") as an ARRAY ref while "strict refs" in use + at line 32.
The L13923 is the first id that I have in my input file...

Replies are listed 'Best First'.
Re^6: How to combine these 2 subroutines in one?
by Athanasius (Chancellor) on Apr 04, 2014 at 08:55 UTC

    The syntax @{ $_[0] } says: read the first argument passed into the sub clean_list, and then, treating it as a reference, dereference it to get an array. If this argument isn’t an array reference (as it probably won’t be if you passed in a list when calling the sub), then of course you’ll get an error message to that effect!

    Just test the first element, that is, call ref( $_[0] ) — and don’t try to dereference it until you’re sure it’s a reference, which is the whole point of testing it by calling ref.

    (I think maybe you should study perlreftut, then perllol, perldsc, and perlref, until you’re comfortable with references in Perl.)

    Hope that helps,

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

      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?


        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,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1081036]
[zentara]: a thread goes into a wait state after emitting a wait to all other threads. It locks up a machine completely.
[marto]: if you mean the recent hyperthreading bug, I don't think that's NSA related?
[zentara]: s/thread/cpu/
[marto]: xkcd://538
[Discipulus]: these opcodes mentioned in the wiki pages are the same opcode perl is translated into? or is just the same term but in different fields?
[zentara]: it's useful if you want to lockup a multicore machine :-)
[zentara]: marto I would give up the key easy with drugs....
[Corion]: Discipulus: No, the Perl opcodes are more high-level than the raw processor opcodes
[zentara]: sodium pentathol and a beautiful woman interogator :-)
[Discipulus]: thanks Corion if so i probably never meet them in my life.. ;=)

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2017-07-28 12:12 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (428 votes). Check out past polls.