blib/lib/Gnuplot/Simple.pm

filter: Statementstmt 100% 27 / 27 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod n/a totaltotal 97.4% 38 / 39 CC 2 SCAR 1.9
CC
2
Cov
97%
BEGIN
2.1
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
↑ Summary
1 package Gnuplot::Simple;
2 $Gnuplot::Simple::VERSION = '0.013';
3 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Types::Standard qw(ArrayRef Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use File::Slurper qw(read_text write_text);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Exporter::Tidy all => [qw(exec_commands write_data)];
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp::Assert;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.6
14 1 ▶ system("which gnuplot > /dev/null") == 0 || die 'Gnuplot was not found from $PATH using which.';
Branch: 1 branch
BranchTrueFalseTotal
system("which gnuplot > /dev/null") == 0 or die "Gnuplot was not found from \$PATH using which." 0 1 1
15 }
16  
17 #ABSTRACT Gnuplot::Simple - A simple way to control Gnuplot
18  
19 =head1 NAME
20  
21 Gnuplot::Simple - A simple way to control Gnuplot
22  
23 =head1 SYNOPSIS
24  
25 Gnuplot can be controlled from Perl using a pipe. This modules faciliates the process
26 to allow more convenient graph plotting from perl e.g.
27  
28 use Gnuplot::Simple qw(write_data);
29  
30 write_data("file.txt", [[ 1,2 ],[ 2,3 ]] );
31  
32 For obvious reasons, gnuplot needs to be installed for this module to work. The presence
33 of gnuplot is checked using the "which" shell command to verify that it is reachable
34 via $PATH.
35  
36 =head1 FUNCTION/ATTRIBUTES
37  
38 =head2 func write_data($filename, $dataset)
39  
40 Write the $dataset to the file $filename to create a gnuplot data file.
41 Each element in $dataset should be of the form [[<c1>...<cn>],...]
42  
43 The column values must not contain newlines (\n or \r) or quote marks. They can be non-ascii unicode.
44  
45 =head2 func exec_commands ($c, $data, $placeholder = "__DATA__")
46  
47 Example usage:
48  
49 use Gnuplot::Simple qw(exec_commands);
50 my $d = [ [ 1,2 ],[ 2,3 ] ];
51 exec_commands(
52 qq{
53 set terminal png
54 set output "myfile.png"
55 plot __DATA__ u 1:2
56 }, $d
57 );
58  
59 The function takes a string of gnuplot commands $c that is piped to gnuplot. You can give a data set as well in the array ref $data.
60 Then, any occurences of __DATA__ in $c are replaced by a temp file containing $data transformed to gnuplot format as
61 done by write_data. The placeholder __DATA__ can be changed via the last parameter.
62  
63 The function throws the gnuplot error message if execution fails.
64  
65 =head1 LICENSE
66  
67 This software is licensed under the same terms as Perl itself.
68  
69 =cut
70  
71 func _is_valid (Str $r) {
72 $r =~ /^[^\n\r"]*\z/;
73 }
74  
75 func _check_constraint ( ArrayRef[ArrayRef] $data ) {
76 die 'Non-empty data not accepted'
77 unless @$data;
78 for (@$data) {
79 assert @$_ > 0, "There must be more than one column";
80 assert _is_valid( join "", @$_ ), '\n, \r or " chars not allowed in values.';
81 }
82 }
83  
84 func write_data ( Str $filename, $data ) {
85 _check_constraint($data);
86 write_text( $filename, _transform($data) );
87 }
88  
89 func _transform ( ArrayRef $data) {
90 my $csv = Text::CSV->new( { binary => 1, sep_char => "\t", quote_space => 1 } );
91 join( "\n", map { $csv->combine(@$_); $csv->string(); } @$data );
92 }
93  
94 func exec_commands ( Str $c, $data, $placeholder = "__DATA__" ) {
95  
96 my ( $eh, $errfile ) = tempfile();
97  
98 open my $proc, "|gnuplot 2>$errfile" or die "Could not open pipe to gnuplot.";
99  
100 my $tmp = File::Temp->new();
101 my $tfn = $tmp->filename();
102 write_data( $tfn, $data );
103  
104 #use placeholder to replace all occurences
105 $c =~ s/$placeholder/'$tfn'/g;
106  
107 binmode $proc, ":utf8";
108 ( print $proc $c ) or die "Failed to input commands to gnuplot: $!";
109  
110 close($proc);
111  
112 my $err = read_text( $errfile );
113 $err && die "Gnuplot execution failed:\n$err";
114 }
115  
116 1;
117  
↑ Summary