Skip to content

Commit 8477137

Browse files
committed
Code review changes from drgrice1.
Mostly unpacking function calls, blessing objects in a single line, fixing perl calls, along with other code cleanup suggestions.
1 parent a78b2f2 commit 8477137

File tree

5 files changed

+50
-72
lines changed

5 files changed

+50
-72
lines changed

lib/Plots/Axes.pm

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ use warnings;
195195

196196
sub new {
197197
my $class = shift;
198-
my $self = {
198+
my $self = bless {
199199
xaxis => {},
200200
yaxis => {},
201201
styles => {
@@ -206,9 +206,8 @@ sub new {
206206
show_grid => 1,
207207
},
208208
@_
209-
};
209+
}, $class;
210210

211-
bless $self, $class;
212211
$self->xaxis($self->axis_defaults('x'));
213212
$self->yaxis($self->axis_defaults('y'));
214213
return $self;
@@ -249,13 +248,13 @@ sub axis {
249248
}
250249

251250
sub xaxis {
252-
my $self = shift;
253-
return $self->axis('xaxis', @_);
251+
my ($self, @items) = @_;
252+
return $self->axis('xaxis', @items);
254253
}
255254

256255
sub yaxis {
257-
my $self = shift;
258-
return $self->axis('yaxis', @_);
256+
my ($self, @items) = @_;
257+
return $self->axis('yaxis', @items);
259258
}
260259

261260
sub set {

lib/Plots/Data.pm

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,8 @@ use strict;
111111
use warnings;
112112

113113
sub new {
114-
my $class = shift;
115-
my $self = {
116-
name => '',
117-
x => [],
118-
y => [],
119-
function => {},
120-
styles => {},
121-
@_
122-
};
123-
124-
bless $self, $class;
125-
return $self;
114+
my ($class, %options) = @_;
115+
return bless { name => '', x => [], y => [], function => {}, styles => {}, %options }, $class;
126116
}
127117

128118
sub name {
@@ -166,13 +156,13 @@ sub style {
166156
}
167157

168158
sub set_function {
169-
my $self = shift;
159+
my ($self, %options) = @_;
170160
$self->{function} = {
171161
sub_x => sub { return $_[0]; },
172162
sub_y => sub { return $_[0]; },
173163
min => -5,
174164
max => 5,
175-
@_
165+
%options
176166
};
177167
$self->style(steps => $self->{function}{steps}) if $self->{funciton}{steps};
178168
return;
@@ -211,11 +201,11 @@ sub _add {
211201
}
212202

213203
sub add {
214-
my $self = shift;
215-
if (ref($_[0]) eq 'ARRAY') {
216-
for (@_) { $self->_add(@$_); }
204+
my ($self, @points) = @_;
205+
if (ref($points[0]) eq 'ARRAY') {
206+
for (@points) { $self->_add(@$_); }
217207
} else {
218-
$self->_add(@_);
208+
$self->_add(@points);
219209
}
220210
return;
221211
}

lib/Plots/GD.pm

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,13 @@ use warnings;
3030

3131
sub new {
3232
my ($class, $pgplot) = @_;
33-
my $self = {
33+
return bless {
3434
image => '',
3535
pgplot => $pgplot,
3636
position => [ 0, 0 ],
3737
colors => {},
38-
};
39-
bless $self, $class;
40-
41-
$self->{image} = new GD::Image($pgplot->size);
42-
return $self;
38+
image => GD::Image->new($pgplot->size)
39+
}, $class;
4340
}
4441

4542
sub pgplot {
@@ -199,7 +196,7 @@ sub draw_label {
199196

200197
sub draw_arrow_head {
201198
my ($self, $x1, $y1, $x2, $y2, $color, $w) = @_;
202-
return unless scalar(@_) > 4;
199+
return unless @_ > 4;
203200
$color = $self->color($color || 'default_color');
204201
$w = 1 unless $w;
205202
($x1, $y1) = ($self->im_x($x1), $self->im_y($y1));
@@ -214,7 +211,7 @@ sub draw_arrow_head {
214211
my $py = $ux;
215212
my $hbx = $x2 - 7 * $w * $ux;
216213
my $hby = $y2 - 7 * $w * $uy;
217-
my $head = new GD::Polygon;
214+
my $head = GD::Polygon->new;
218215
$head->addPt($x2, $y2);
219216
$head->addPt($hbx + 3 * $w * $px, $hby + 3 * $w * $py);
220217
$head->addPt($hbx - 3 * $w * $px, $hby - 3 * $w * $py);

lib/Plots/Plot.pm

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ use Plots::Tikz;
3232
use Plots::GD;
3333

3434
sub new {
35-
my ($class, $pg, @opts) = @_;
35+
my ($class, $pg, %options) = @_;
3636
my $size = $main::envir{onTheFlyImageSize} || 500;
3737

38-
my $self = {
38+
my $self = bless {
3939
pg => $pg,
4040
imageName => {},
4141
type => 'Tikz',
@@ -44,10 +44,9 @@ sub new {
4444
axes => Plots::Axes->new,
4545
colors => {},
4646
data => [],
47-
@opts
48-
};
47+
%options
48+
}, $class;
4949

50-
bless $self, $class;
5150
$self->color_init;
5251
return $self;
5352
}
@@ -57,18 +56,12 @@ sub colors {
5756
return defined($color) ? $self->{colors}{$color} : $self->{colors};
5857
}
5958

60-
sub _add_color {
61-
my ($self, $color, $r, $g, $b) = @_;
62-
$self->{'colors'}{$color} = [ $r, $g, $b ];
63-
return;
64-
}
65-
6659
sub add_color {
67-
my $self = shift;
68-
if (ref($_[0]) eq 'ARRAY') {
69-
for (@_) { $self->_add_color(@$_); }
60+
my ($self, @colors) = @_;
61+
if (ref($colors[0]) eq 'ARRAY') {
62+
for (@colors) { $self->{colors}{ $_->[0] } = [ @$_[ 1 .. 3 ] ]; }
7063
} else {
71-
$self->_add_color(@_);
64+
$self->{colors}{ $colors[0] } = [ @colors[ 1 .. 3 ] ];
7265
}
7366
return;
7467
}
@@ -98,7 +91,10 @@ sub size {
9891
sub data {
9992
my ($self, @names) = @_;
10093
return wantarray ? @{ $self->{data} } : $self->{data} unless @names;
101-
my @data = grep { my $name = $_->name; grep(/^$name$/, @names) } @{ $self->{data} };
94+
my @data = grep {
95+
my $name = $_->name;
96+
grep {/^$name$/} @names
97+
} @{ $self->{data} };
10298
return wantarray ? @data : \@data;
10399
}
104100

@@ -161,15 +157,15 @@ sub image_type {
161157
# Tikz needs to use pdf for hardcopy generation.
162158
sub ext {
163159
my $self = shift;
164-
return 'pdf' if ($self->{type} eq 'Tikz' && $main::displayMode eq 'TeX');
160+
return 'pdf' if ($self->{type} eq 'Tikz' && eval('$main::displayMode') eq 'TeX');
165161
return $self->{ext};
166162
}
167163

168164
# Return a copy of the tikz code (available after the image has been drawn).
169165
# Set $plot->{tikzDebug} to 1 to just generate the tikzCode, and not create a graph.
170166
sub tikz_code {
171167
my $self = shift;
172-
return ($self->{tikzCode} && $main::displayMode =~ /HTML/) ? '<pre>' . $self->{tikzCode} . '</pre>' : '';
168+
return ($self->{tikzCode} && eval('$main::displayMode') =~ /HTML/) ? '<pre>' . $self->{tikzCode} . '</pre>' : '';
173169
}
174170

175171
# Add functions to the graph.
@@ -298,11 +294,11 @@ sub _add_dataset {
298294
}
299295

300296
sub add_dataset {
301-
my $self = shift;
302-
if (ref($_[0]) eq 'ARRAY' && ref($_[0]->[0]) eq 'ARRAY') {
303-
return [ map { $self->_add_dataset(@$_); } @_ ];
297+
my ($self, @data) = @_;
298+
if (ref($data[0]) eq 'ARRAY' && ref($data[0][0]) eq 'ARRAY') {
299+
return [ map { $self->_add_dataset(@$_); } @data ];
304300
}
305-
return $self->_add_dataset(@_);
301+
return $self->_add_dataset(@data);
306302
}
307303

308304
sub _add_label {
@@ -324,8 +320,8 @@ sub _add_label {
324320
}
325321

326322
sub add_label {
327-
my $self = shift;
328-
return ref($_[0]) eq 'ARRAY' ? [ map { $self->_add_label(@$_); } @_ ] : $self->_add_label(@_);
323+
my ($self, @labels) = @_;
324+
return ref($labels[0]) eq 'ARRAY' ? [ map { $self->_add_label(@$_); } @labels ] : $self->_add_label(@labels);
329325
}
330326

331327
# Fill regions only work with GD and are ignored in TikZ images.
@@ -339,8 +335,11 @@ sub _add_fill_region {
339335
}
340336

341337
sub add_fill_region {
342-
my $self = shift;
343-
return ref($_[0]) eq 'ARRAY' ? [ map { $self->_add_fill_region(@$_); } @_ ] : $self->_add_fill_region(@_);
338+
my ($self, @regions) = @_;
339+
return
340+
ref($regions[0]) eq 'ARRAY'
341+
? [ map { $self->_add_fill_region(@$_); } @regions ]
342+
: $self->_add_fill_region(@regions);
344343
}
345344

346345
sub _add_stamp {
@@ -358,8 +357,8 @@ sub _add_stamp {
358357
}
359358

360359
sub add_stamp {
361-
my $self = shift;
362-
return ref($_[0]) eq 'ARRAY' ? [ map { $self->_add_stamp(@$_); } @_ ] : $self->_add_stamp(@_);
360+
my ($self, @stamps) = @_;
361+
return ref($stamps[0]) eq 'ARRAY' ? [ map { $self->_add_stamp(@$_); } @stamps ] : $self->_add_stamp(@stamps);
363362
}
364363

365364
# Output the image based on a configurable type:

lib/Plots/Tikz.pm

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,16 @@ use warnings;
2828

2929
sub new {
3030
my ($class, $pgplot) = @_;
31-
my $image = new LaTeXImage;
31+
my $image = LaTeXImage->new;
3232
$image->environment('tikzpicture');
33-
$image->svgMethod($main::envir{latexImageSVGMethod} // 'pdf2svg');
33+
$image->svgMethod($main::envir{latexImageSVGMethod} // 'dvisvgm');
3434
$image->convertOptions($main::envir{latexImageConvertOptions} // { input => {}, output => {} });
3535
$image->ext($pgplot->ext);
36-
$image->tikzLibraries('arrows.meta');
36+
$image->tikzLibraries('arrows.meta,plotmarks');
3737
$image->texPackages(['pgfplots']);
3838
$image->addToPreamble('\pgfplotsset{compat=1.18}\usepgfplotslibrary{fillbetween}');
3939

40-
my $self = {
41-
image => $image,
42-
pgplot => $pgplot,
43-
colors => {},
44-
};
45-
bless $self, $class;
46-
47-
return $self;
40+
return bless { image => $image, pgplot => $pgplot, colors => {} }, $class;
4841
}
4942

5043
sub pgplot {

0 commit comments

Comments
 (0)