Hello Monks,

I am trying to enlighten myself by reading as much code on here as possible and figuring out the parts that I do not understand. I came across this post Networking Perl today and Your Mother's reply had the following code
use warnings; use strict; use JSON; use Plack::Request; use Proc::ProcessTable; sub { my $req = Plack::Request->new(+shift); my %pids = map { $_ => 1 } $req->path =~ /(\d+)/g; my $procs = Proc::ProcessTable->new; my $json = JSON->new; my %status; for my $proc ( @{ $procs->table } ) { next if scalar(%pids) and not $pids{$proc->pid}; for my $field ( $procs->fields ) { $status{$proc->pid}{$field} = $proc->$field; } } return [ 200, [ "Content-Type" => "application/json" ], [ $json->pretty->encode(\%status) ] ]; };
There are two items in this that are giving me trouble. First this map, I don't quite understand what it is doing (it seems no matter how much I read about map I still can't quite grasp it):
my %pids = map { $_ => 1 } $req->path =~ /(\d+)/g;
Is it saying "For each item found in $req->path that is a number save it as a hash key of %pids?"

The other thing that confuses me is this
next if scalar(%pids) and not $pids{$proc->pid};
I know "scalar(%pids)" will force the hash %pids to be interpreted in scalar context, but to be honest with you I do not know exactly what this will do to the hash. I tried testing this on my own with the following code:
my %hash = ( fred => flintstone, wilma => flintstone, barney => ruble, ); print scalar(%hash) . "\n";
But this prints out "3/8" so I'm really confused.

Thank you for helping me on my journey.

Thanks,
Dru

Perl, the Leatherman of Programming languages. - qazwart

In reply to Need Help in Understanding Some Code - Map and Scalar Questions by Dru

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.