4

So I made this small script today in perl.

For some reason it doesn't seem to be downloading anything, and an error keeps popping up saying

Use of uninitialized value $id in concatenation (.) or string at room.pl line 18.
Use of uninitialized value $id in concatenation (.) or string at room.pl line 18.

Could someone help me fix this code?

Also is using File::Path okay? and this is the json http://media1.clubpenguin.com/play/en/web_service/game_configs/rooms.json

use strict;
use warnings;

use File::Path;

mkpath "rooms/";

use JSON;
use LWP::Simple qw(mirror);

open FILE, 'rooms.json' or die "Could not open file inputfile: $!";
sysread(FILE, my $result, -s FILE);
close FILE or die "Could not close file: $!"; 
my $json = decode_json($result);

foreach $item ($json) {
my $id = $item->{room_key};
mirror "http://media1.clubpenguin.com/play/v2/content/global/rooms/$id.swf" => "rooms/$id.swf";
}
4

1 回答 1

3

foreach my $item ...在第 16 行应该可以解决问题!

作为它的哈希引用,您必须以这种方式循环它:

...
foreach my $item (sort keys %$json) {
        my $id = $json->{$item}->{room_key};
        print $id . "\n";
        #mirror "http://media1.clubpenguin.com/play/v2/content/global/rooms/$id.swf" => "rooms/$id.swf";
    }
于 2013-08-12T11:19:44.477 回答