Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I have a Perl script that generates a huge hash tree. The Perl script is called from C# and it works fine. What I need to do now is to receive the returning hash from Perl in C#.

How can I do it?

Thanks :)

Replies are listed 'Best First'.
Re: how to get Perl hash in C#
by tobyink (Canon) on Jan 24, 2014 at 10:06 UTC

    Have your Perl program dump the output as JSON (use either JSON, JSON::MaybeXS, JSON::Tiny, or JSON::PP - the last of which comes out of the box with recent versions of Perl) either to STDOUT or to a temporary file.

    Then get your C# program to read the JSON and parse it. (There are several JSON libraries for C# listed on json.org.)

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
      OK,

      When I try to run this:

      my $JSON = JSON->new->utf8; $JSON->convert_blessed(1); $JSON->allow_blessed(1); $JSON->pretty(1); my $json = $JSON->encode(\%arr_hash); print "JSON [\n $json\n ]\n";
      I only get the root level data and the rest are null!
      JSON [ { "ace_context" : [ null, null, null, null, null, null, null, null ], "server_nmap" : [ null, null, ... null ] }
      Same thing happends when I try this:
      my $json = $JSON->encode(\@ace_context);
      BUT when I run this:

      print Dumper(\%arr_hash);

      the output contains everything.

      How the JSON code should look like in order to "export" everything to JSON-format data?

        What is this "everything" that appears in the Data::Dumper output? Is it stuff that would be nonsensical in JSON output? (e.g. coderefs, pointers to open file handles, etc)

        use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: how to get Perl hash in C#
by davido (Cardinal) on Jan 24, 2014 at 16:45 UTC
Re: how to get Perl hash in C#
by Hossein (Acolyte) on Jan 24, 2014 at 10:02 UTC

    This is the hash

    my %arr_hash = ( fw_fw => \@fw_fw, nexus_vrf => \@nexus_vrf, nexus_vlan => \@nexus_vlan, nexus_portchannel => \@nexus_portchannel, ace_context => \@ace_context, ace_interface => \@ace_interface, ace_rserver => \@ace_rserver, ace_serverfarm => \@ace_serverfarm, asa_interface => \@asa_interface, asa_route => \@asa_route, server_nmap => \@server_nmap, server_ucmdb => \@server_ucmdb );

      The easiest way would be to print the hash as a JSON structure and then to read that structure from C#.