in reply to RFC: pianobar event example

Just to save some typing you could have written :-

$line = '"'.$data{title}.'","'.$data{album}.'","'.$data{artist}.'" +,"'.$data{songStationName}.'"'; if defined($data{coverArt}) { $line .= ',"' . $data{coverArt} . '"' ; } $line .= "\n";

But building CSV lines & quoting fields always gets a bit messy, so you might like to look at Text::CSV which knows how to get it right.

Then your code could look something like this :-

my @values = map { $data{$_} } qw/title album artist .../; $csv->combine(@values); $line = $csv->string();

Replies are listed 'Best First'.
Re^2: RFC: pianobar event example
by soonix (Chancellor) on Aug 14, 2014 at 13:11 UTC
    could even save more typing with a hash slice:
    $line = join',', map {qq/"$_"/} @data{qw/title album artist songSt +ationName/};
    although perhaps looking more cryptic, makes inserting and rearranging the columns easier.
    Your Text::CSV example would have
    $csv->combine(@data{qw/title album artist songStationName/});