I have a Perl hash that I need to write to JSON and to CSV formats.

A print Dumper( \%formdata ) of my hash looks like this.

$VAR1 = { 'SPRequest' => { 'xrelease' => '13038', 'macaddr' => '47:00:11:22:00:30', 'name' => 'localhost', 'description' => 'demo' }, '.submit' => bless( do{\(my $o = 1)}, 'JSON::PP::Boolean +' ), 'class' => 'SPRequest', '22406' => { 'win.profile' => 'production', 'win.os_version' => 'standard', 'win.os_part_size' => '1' } };

Here is the snipplet of code I use to produce my json file.

my $form_data_file = "/tmp/${hostname}_${macaddr}.json"; open FH, ">$form_data_file" or die "Could not open $form_data_file. :$ +!\n"; print FH to_json( \%formdata, {pretty=>1} ); close FH;

I am able to output my JSON to a file which looks like this:

[red@tools-dev1 psong]$ cat /tmp/localhost_47-00-11-22-00-30.json { "SPRequest" : { "xrelease" : "13038", "macaddr" : "47:00:11:22:00:30", "name" : "localhost", "description" : "demo" }, ".submit" : true, "class" : "SPRequest", "22406" : { "win.profile" : "production", "win.os_version" : "standard", "win.os_part_size" : "1" } }

Here is the code I am using to create my CSV file:

my $form_data_file_csv = "/tmp/${hostname}_${macaddr}.csv"; # Text::CSV::Slurp wants arrayref of hashref my $ARoHR = [ \%formdata ]; my $csv = Text::CSV::Slurp->create( input => $ARoHR); open FH, ">$form_data_file_csv" or die "Could not open $form_data_file +_csv. :$!\n"; print FH $csv; close FH;

But the problem is my CSV file which ends up looking like this:

[red@tools-dev1 psong]$ cat /tmp/localhost_47-00-11-22-00-30.csv ; ech +o .submit,22406,SPRequest,class true,HASH(0x8d81918),HASH(0x8d67980),SPRequest

What am I doing wrong here?


In reply to Convert hash to CSV using Text::CSV::Slurp by Plankton

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.