Question

I'm trying to use TMDB - Perl wrapper for The MovieDB API http://metacpan.org/pod/TMDB and i'm stuck

I can't work out how to extract the components.

for example given

  #!/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>"; 

    }

I get this

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

from the cpan page it says

print Dumper $movie->cast;

which gives

$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
    };

repeated for each actor

how do i convert it to something like

my $name = 'Gemma Arterton';
my $id = '59620';
Was it helpful?

Solution

You're almost there

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

    print "<p>name $name</p>";
    print "<p>id $id</p>";
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top