如何使用TMDB - 用于MovieDB API的Perl包装器

时间:2013-07-12 16:57:14

标签: perl themoviedb-api

我正在尝试使用TMDB - 用于MovieDB API {2}的Perl包装器而且我被卡住了

我无法弄清楚如何提取组件。

例如给定

  #!/usr/bin/perl   
  use TMDB;
  use Data::Dumper qw(Dumper); 
  use CGI;
  use JSON;

  my $cgi = CGI->new();
  my $tmd= $cgi->param('tmd');

  # Initialize
  $tmd = "60304";
  my $tmdb = TMDB->new( apikey => 'XXXXXXXXXXX' );

  # HTTP HEADER

  print "Content-type:text/html\n\n"; 

  # Movie Data
  # ===========
    my $movie = $tmdb->movie( id =>  $tmd );


  my @cast = $movie->cast;
  foreach my $actor (@cast)
    {  
     print "<p>name " . Dumper($actor) . "</p>"; 

    }

我明白了

name $VAR1 = { 'cast_id' => 2, 'order' => 0, 'character' => 'Gretel', 'name' => 'Gemma Arterton', 'profile_path' => '/vOzCQfZOSYLdGjvyD7XDvCzXN4s.jpg', 'id' => 59620 }; 

来自cpan页面

print Dumper $movie->cast;

给出了

$VAR1 = {
      'cast_id' => 2,
      'order' => 0,
      'character' => 'Gretel',
      'name' => 'Gemma Arterton',
      'profile_path' => '/vOzCQfZOSYLdGjvyD7XDvCzXN4s.jpg',
      'id' => 59620
    };
$VAR2 = {
      'cast_id' => 3,
      'order' => 1,
      'character' => 'Hansel',
      'name' => 'Jeremy Renner',
      'profile_path' => '/l6CxIOFCjF65298teEJd5mCnPDO.jpg',
      'id' => 17604
    };

为每个演员重复

如何将其转换为类似

的内容
my $name = 'Gemma Arterton';
my $id = '59620';

1 个答案:

答案 0 :(得分:1)

你快到了

foreach my $actor (@cast) {
    $name = $actor->{name};
    $id = $actor->{id};
    ...

    print "<p>name $name</p>";
    print "<p>id $id</p>";
    ...
}