I'm reading through the 3rd part of the beginner's tutorial (http://news.oreilly.com/2008/06/a-beginners-introduction-to-pe.html) and I have a "what if" question. Towards the bottom, there is a piece of code you can look at that parses and summarizes an http log file. One of the functions in it is:
sub report_section { my ( $header, %types ) = @_; say $header; for my $type ( sort keys %types ) { say "$type: $types{$type}"; } }
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?

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?


In reply to Passing a scalar and hash as subroutine arguments by jktstance

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.