Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The program can be broken down into three steps:</p> <ol> <li>Fetch the JSON source.</li> <li>Parse the JSON.</li> <li>Iterate over decoded data structure. We expect an array of hashes. Mirror files denoted by the <code>paper_item_id</code> to the working directory.</li> </ol> <p>We will use <code>LWP::Simple</code> functions here.</p> <p>Our script has the following header:</p> <pre><code>#!/usr/bin/perl use strict; # disallow bad constructs use warnings; # warn about possible bugs use LWP::Simple; use JSON; </code></pre> <h3>Fetching the JSON</h3> <pre><code>my $json_source = get "http://media1.clubpenguin.com/play/en/web_service/game_configs/paper_items.json"; die "Can't access the JSON source" unless defined $json_source; </code></pre> <p>That was easy: we dispatch a <code>get</code> request on that URL. If the output is undefined, we throw a fatal exception.</p> <h3>Parsing the JSON</h3> <pre><code>my $json = decode_json $json_source; </code></pre> <p>That was easy; we expect the <code>$json_source</code> to be an UTF-8 encoded binary string.</p> <p>If we want to inspect what is inside that data structure, we can print it out like</p> <pre><code>use Data::Dumper; print Dumper $json; </code></pre> <p>or</p> <pre><code>use Data::Dump; dd $json; </code></pre> <p>If everything works as expected, this should give a screenfull of an array of hashes.</p> <h3>Iterating</h3> <p>The <code>$json</code> is an array reference, so we'll loop over all items:</p> <pre><code>my $local_path = "paper"; my $server_path = "http://media1.clubpenguin.com/play/v2/content/global/clothing/paper"; for my $item (@$json) { my $filename = "$item-&gt;{paper_item_id}.swf"; my $response = mirror "$server_path/$filename" =&gt; "$local_path/$filename"; warn "mirror failed for $filename with $response" unless $response == 200; } </code></pre> <p>Perl has a concept of <em>references</em>, which is similar to pointers. Because data structures like hashes or arrays can only contain scalars, other arrays or hashes are only referenced. Given an array reference, we can access the array like <code>@$reference</code> or <code>@{ $reference }</code>.</p> <p>To access an entry, the subscript operator <code>[...]</code> for arrays or <code>{...}</code> for hashes is seperated by the dereference operator <code>-&gt;</code>.</p> <p>Thus, given <code>%hash</code> and <code>$hashref</code> to the same hash,</p> <pre><code>my %hash = (key =&gt; "a", otherkey =&gt; "b"); my $hashref = \%hash; </code></pre> <p>then <code>$hashref-&gt;{key} eq $hash{key}</code> holds.</p> <p>Therefore, we loop over the items in <code>@$json</code>. All of these items are hash references, therefore we use <code>$item-&gt;{$key}</code>, not <code>$hash{key}</code> syntax.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload