title: Read a Web Service output and write it to CSV in Perl
body: <p>I am trying to read the output of webservice and write to a csv file. The source has many fields and it is quoted with comma delimiter.
  A few columns have values on a new line too.
  213,&quot;Dan&quot;,&quot;This is kool&quot;,&quot;dblee&quot;
  214,&quot;Taylot&quot;,&quot;This is,
  not mine&quot;,&quot;typelee&quot;</p>
  <p>Output
  213,&quot;Dan&quot;,&quot;dblee&quot;
  214,&quot;Taylot&quot;,&quot;typelee&quot;</p>
  <p>Here I am trying to use get to capture of the output of WS. But facing a problem while trying to read individual fields.This ($csv-&gt;getline($fh1)) is not returning an array.
  How to make it to return an array. Any inputs.</p>
  <pre><code>use Getopt::Long; 
  use File::Copy; 
  use Text::CSV; 
  
  my $filename  = '/mydir/file.txt'; 
  my $filename2 = '/mydir/file_formatted.csv'; 
  
  unlink $filename; 
  
  for ( my $i = 1 ; $i &lt;= 2 ; $i++ ) 
  { 
     $WS_URL = &quot;https://adesfhr.com/wpi.php?cmd=dsd&amp;PageNumber=$i&amp;pageSize=10&amp;responseType=csv&quot;; `
  my $resp = $SSO-&gt;get($WS_URL); `
  my $data = $resp-&gt;content(); `
  last if ! $data; `
  if ($data) { 
        my $csv = Text::CSV-&gt;new( 
         { 
            binary    =&gt; 1,      # Allow special character. Always set this 
               auto_diag =&gt; 1,      # Report irregularities immediately 
               sep_char  =&gt; ',',    #Separatrp 
           } 
       ); 
       open( my $fh1, &quot;&lt;:encoding(utf8)&quot;, \$data ) or die &quot;$!&quot;; 
       open( my $fh2, '&gt;&gt;', $filename ) 
         or die &quot;Could not open file '$filename' $!&quot;; 
  
  &lt;$fh1&gt;; 
       while ( my $row = $csv-&gt;getline($fh1) ) { 
  
               my $newrow = splice( @$row, 0, 3 ); 
               print &quot;@$newrow\n&quot;; 
               $csv-&gt;print( $fh2, $newrow ); 
       } 
       close $fh2; 
       close $fh1; 
    } 
  }
  </code></pre>