From 410b1363b6f1c93a98937cae8d1ed2b31cf19910 Mon Sep 17 00:00:00 2001 From: tim Date: Sun, 17 Dec 2017 11:06:14 +0200 Subject: [PATCH] Manage gnuplot with IPC::Open3, so we allow fancy output filenames, with spaces! --- glucometer_graphs.pl | 53 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 12 deletions(-) diff --git a/glucometer_graphs.pl b/glucometer_graphs.pl index 5cec335..3c52f61 100644 --- a/glucometer_graphs.pl +++ b/glucometer_graphs.pl @@ -3,6 +3,7 @@ use strict; use warnings; use Getopt::Long; +use IPC::Open3; use Time::Piece; my $error = "Usage: $0 --input --output [--max ] [--low ] [--high ]\n"; @@ -27,19 +28,17 @@ GetOptions ("input=s" => \$input, # The name of the CSV file from whic "low:f" => \$min_glucose, # The low end of your target blood glucose level "max:i" => \$graph_max, # The highest displayed glucose level on each graph "graphs:i" => \$days_per_page) # The number of days printed on each page - or die $error; + or die $error; -die $error unless ( defined $input and defined $output ); - -open( my $fh, '<:encoding(UTF-8)', $input ) +open( my $ifh, '<:encoding(UTF-8)', $input ) or die "Could not open file '$input' $!"; -while ( my $row = <$fh> ) { +while ( my $row = <$ifh> ) { chomp( $row ); push @lines, $row; } -close( $fh ) +close( $ifh ) or warn "close failed: $!"; # Set up basic gnuplot options for reading the CSV data @@ -98,7 +97,7 @@ reset set datafile separator whitespace set terminal pdf size 29.7cm,21.0cm enhanced font 'Calibri,14' linewidth 1 -set output '$output' +#set output '$output' set key off set style data lines set xdata time @@ -134,7 +133,7 @@ foreach my $d ( sort keys %seen ) { push @data, qq( set title "Daily Glucose Summary for $title" font "Calibri,18" -set xlabel "Time" offset 0,1 +set xlabel "Time" offset 0,-0.25 set ylabel "Blood glucose" set xtics left tc rgb "#000000" set ytics 2 tc rgb "#000000" @@ -152,7 +151,7 @@ undefine \$SmoothData$label # Add an x grid set multiplot previous set title " " -set xlabel " " offset 0,1 +set xlabel " " offset 0,-0.25 set ylabel " " set xtics tc rgb "#ffffff00" set ytics tc rgb "#ffffff00" @@ -173,9 +172,39 @@ push @data, qq( unset multiplot ); -# output the gnuplot data +# run the data through gnuplot $gnuplot_data = join "\n", @data; -print $gnuplot_data; -#gnuplot(@data); + +open( my $ofh, '>', $output ) + or die "Could not open file '$output' $!"; + +my ( $pid, $stdin, $stdout, $stderr ); +use Symbol 'gensym'; +$stderr = gensym; + +$pid = open3( $stdin, $stdout, $stderr, 'gnuplot' ); + +print $stdin $gnuplot_data; +close( $stdin ); + +while ( <$stdout> ) { + print $ofh "$_"; +} + +while ( <$stderr> ) { + warn $_; +} + +close($stdout); +close($stderr); + +waitpid( $pid, 0 ); +my $child_exit_status = $? >> 8; + +close( $ofh ) + or warn "close failed: $!"; + +#open (GNUPLOT, "|gnuplot"); +#print GNUPLOT $gnuplot_data; # vim: set expandtab shiftwidth=4 softtabstop=4 :