-1

I'm currently working with an API, and I wrote a short script to get details of a project I created. I'm able tu successfuly display the whole content with Data::Dumper but I cannot display a specific element or assign it to a variable. Here is my short code :

# !/usr/bin/perl
use strict;
use warnings;
use TestLink::API;
use Data::Dumper;
my $tl=TestLink::API->new('http://127.0.0.1/testlink-1.9.16/lib/api/xmlrpc/v1/xmlrpc.php', 'a64aaa61db4e9f65d4e52745b86e3d8f');
print Dumper($tl->getProjectByName('Created_With_API'));
my %hachage = $tl->getProjectByName('Created_With_API');

And this code display my information, ie :

$VAR1 = {
          'prefix' => 'CWAPI',
          'notes' => 'res ipsa loquiter',
          'opt' => {
                     'inventoryEnabled' => '1',
                     'testPriorityEnabled' => '1',
                     'automationEnabled' => '1',
                     'requirementsEnabled' => '1'
                   },
          'options' => 'O:8:"stdClass":4:{s:19:"requirementsEnabled";i:1;s:19:"testPriorityEnabled";i:1;s:17:"automationEnabled";i:1;s:16:"inventoryEnabled";i:1;}',
          'color' => '',
          'option_priority' => '0',
          'issue_tracker_enabled' => '0',
          'id' => '14',
          'is_public' => '1',
          'option_reqs' => '0',
          'type' => 'project',
          'api_key' => '9bc99494a418140a1a625257da91d9f855b452c05f498ac2db94cbbbb331db58',
          'name' => 'Created_With_API',
          'option_automation' => '0',
          'reqmgr_integration_enabled' => '0',
          'tc_counter' => '0',
          'active' => '1'
        };

But I can't display a specific element, when I write

print "$hachage{id}";

I get an error. I KNOW, I'm probably doing something wrong, I started learning Perl a few days ago for this API and there must be awfull things. But I'd just like to know what am I doing wrong. Thanks for your help for those who will take time to answer me ;)

Rémi Bosgaerd
  • 150
  • 1
  • 12
  • "*I started learning Perl a few days ago*" ... At this point, you should concentrate on [learning Perl](http://shop.oreilly.com/product/0636920049517.do) instead of delving into third party APIs. – Sinan Ünür Jul 25 '17 at 11:44

2 Answers2

2

getProjectByName doesn't return a list of key/value pairs (which is what you would need to initialize a %hash), it returns a single value, which is a reference to a hash:

my $hachage = $tl->getProjectByName('Created_With_API');

You can access single elements using e.g. $hachage->{id}. See perldoc perlreftut for more information.

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • Thank you, that's why it didn't work, the API documentation was saying "Returns desired project def hash, false otherwise.", and because I don't realy know Perl I imagined it returned the hash, not its reference. – Rémi Bosgaerd Jul 25 '17 at 09:11
0

You will need to create initialise your variable like this

my $hachage = $tl->getProjectByName('Created_With_API');

(Note dollar sign, not percent)

and you can get what you want by doing print "$hachage->{'id'}"

You need this syntax because $tl->getProjectByName returns a reference to a hash, not an actual hash.

The difference is discussed here.

user1717259
  • 2,717
  • 6
  • 30
  • 44