jktstance has asked for the wisdom of the Perl Monks concerning the following question:
I'm confused about data types here. Isn't the @_ variable an array? If so, then is the %types hash converted to a flat list or string when passed?sub report_section { my ( $header, %types ) = @_; say $header; for my $type ( sort keys %types ) { say "$type: $types{$type}"; } }
Now, what if I wanted to reference the @_ variable directly, instead of reassigning its parts to variables as is done here. Obviously to print the header, I just write say $_[0];, but how do I handle a passed hash?
I think the answer here is to use references, but I haven't learned much about them yet and I'm wondering if there's a way to do this without them.
EDIT: Some more testing shows me that @_ is a flattened list. @_1 .. $#_ should give me an array that is the %types hash. I tried to substitute sort keys @_[1 .. $#_] instead of sort keys %types, but since I'm only running Perl version 5.8, the keys function does not work. How wrong am I?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing a scalar and hash as subroutine arguments
by AnomalousMonk (Archbishop) on Jan 24, 2014 at 22:21 UTC | |
|
Re: Passing a scalar and hash as subroutine arguments
by Laurent_R (Canon) on Jan 25, 2014 at 18:48 UTC | |
|
Re: Passing a scalar and hash as subroutine arguments
by Anonymous Monk on Jan 25, 2014 at 01:07 UTC | |
by Preceptor (Deacon) on Jan 25, 2014 at 09:47 UTC |