If you'll excuse me saying this (which you won't) but somebody is taking the p*** outta you, if this is what they showed you to do (which they probably didn't).

You call a sub, passing in a hashref \%pi.

In the sub, you assign a bunch of values to local disrete scalars.

You then assign these scalars to a hash, using a hash slice.

You then extract the values from the hash (via the hash slice), and return them as a list.

The calling code then assign the list to another bunch of discrete scalars which you then ignore and access the values via the hash?

This isn't just inefficient, it's {expletive}.

And what do you achieve by passing \$site, just so you can do $$site_ref? Your passing one scalar instead of another and dereferencing it twice. Totally useless use of a references.

Here are four different ways of doing what your doing, each of which is simpler, clearer and more efficient. Try and understand how they each work, and then pick one.

No hash

my ($status, $content, $type, $base, $length) = get_page($site); print $length, " bytes\n"; #################################################### sub get_page { my ($site, $pi) = @_; $browser = LWP::UserAgent->new() unless $browser; my $response = $browser->get($site); my $status = $response->status_line; my $content = $response->content; my $type = $response->content_type; my $base = $response->base; my $length = commify(length($content)); if (!$response->is_success) { die "\nError: $status ($site)\n"; } return $status, $content, $type, $base, $length; }

Assign to a hash slice in the calling code from the returned the list.

my %pi; @%pi{qw(status content type base length)) = get_page($site); print $length, " bytes\n"; #################################################### sub get_page { my ($site) = @_; $browser = LWP::UserAgent->new() unless $browser; my $response = $browser->get($site); if (!$response->is_success) { die "\nError: $status ($site)\n"; } return $response->status_line, $response->content, $response->content_type, $response->base, commify(length($content)); }

Assign direct to the hash. No lists.

my %pi; get_page($site, \%pi); print $pi{'length'}, " bytes\n"; #################################################### sub get_page { my ($site, $pi) = @_; $browser = LWP::UserAgent->new() unless $browser; my $response = $browser->get($site); $pi->{status} = $response->status_line; $pi->{content} = $response->content; $pi->{type} = $response->content_type; $pi->{base} = $response->base; $pi->{length} = commify(length($pi->{content})); if (!$response->is_success) { die "\nError: $status ($site)\n"; } }

Assign to hash slice direct. Don't return a list

my %pi; get_page($site, \%pi); print $pi{'length'}, " bytes\n"; #################################################### sub get_page { my ($site, $pi) = @_; $browser = LWP::UserAgent->new() unless $browser; my $response = $browser->get($site); if (!$response->is_success) { die "\nError: $status ($site)\n"; } my $content = $response->content; @{$pi}{qw(status content type base length)} = ( $response->status_line; $content; $response->content_type; $response->base; commify(length($content)); ); return; }

In reply to Re: Re: Optimizing the use of hashes in subroutines by Anonymous Monk
in thread Optimizing the use of hashes in subroutines by Anonymous Monk

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.