Skip to content

Commit b0f1b49

Browse files
committed
Scalar functions shouldn't return empty list
address, deobjectify_text, detach, is_inside, & pindex Change zz_perlcritic.t to exclude ProhibitExplicitReturnUndef
1 parent ac9106d commit b0f1b49

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

Changes

+7
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ Changelog for HTML-Tree
22

33
{{$NEXT}}
44

5+
[THINGS THAT MAY BREAK YOUR CODE OR TESTS]
6+
* Some methods normally returning a scalar could return the empty
7+
list in certain circumstances. This has been corrected. The
8+
affected methods are: address, deobjectify_text, detach, is_inside,
9+
& pindex.
10+
511
[DOCUMENTATION]
612
* Improve SEE ALSO for HTML::TreeBuilder
713
* General documentation cleanup
814

15+
916
4.900 2012-06-01
1017
Trial Release by Christopher J. Madsen
1118

lib/HTML/Element.pm

+14-17
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,6 @@ sub splice_content {
10341034
my ( $self, $offset, $length, @to_add ) = @_;
10351035
Carp::croak "splice_content requires at least one argument"
10361036
if @_ < 2; # at least $h->splice_content($offset);
1037-
return $self unless @_;
10381037

10391038
my $content = ( $self->{'_content'} ||= [] );
10401039

@@ -1077,7 +1076,7 @@ its parent are explicitly destroyed.
10771076

10781077
sub detach {
10791078
my $self = $_[0];
1080-
return unless ( my $parent = $self->{'_parent'} );
1079+
return undef unless ( my $parent = $self->{'_parent'} );
10811080
$self->{'_parent'} = undef;
10821081
my $cohort = $parent->{'_content'} || return $parent;
10831082
@$cohort = grep { not( ref($_) and $_ eq $self ) } @$cohort;
@@ -1276,7 +1275,7 @@ sub delete_content {
12761275
12771276
Detaches this element from its parent (if it has one) and explicitly
12781277
destroys the element and all its descendants. The return value is
1279-
undef.
1278+
the empty list (or C<undef> in scalar context).
12801279
12811280
Before version 5.00 of HTML::Element, you had to call C<delete> when
12821281
you were finished with the tree, or your program would leak memory.
@@ -2603,7 +2602,7 @@ the tag names listed. You can use any mix of elements and tag names.
26032602

26042603
sub is_inside {
26052604
my $self = shift;
2606-
return unless @_; # if no items specified, I guess this is right.
2605+
return 0 unless @_; # if no items specified, I guess this is right.
26072606

26082607
my $current = $self;
26092608

@@ -2667,12 +2666,12 @@ C<< $h->pindex >> returns C<undef>.
26672666
sub pindex {
26682667
my $self = shift;
26692668

2670-
my $parent = $self->{'_parent'} || return;
2671-
my $pc = $parent->{'_content'} || return;
2669+
my $parent = $self->{'_parent'} || return undef;
2670+
my $pc = $parent->{'_content'} || return undef;
26722671
for ( my $i = 0; $i < @$pc; ++$i ) {
26732672
return $i if ref $pc->[$i] and $pc->[$i] eq $self;
26742673
}
2675-
return; # we shouldn't ever get here
2674+
return undef; # we shouldn't ever get here
26762675
}
26772676

26782677
#--------------------------------------------------------------------------
@@ -2827,18 +2826,18 @@ sub address {
28272826
shift @stack;
28282827
}
28292828
else { # absolute addressing
2830-
return unless 0 == shift @stack; # to pop the initial 0-for-root
2829+
return undef unless 0 == shift @stack; # pop the initial 0-for-root
28312830
$here = $_[0]->root;
28322831
}
28332832

28342833
while (@stack) {
2835-
return
2834+
return undef
28362835
unless $here->{'_content'}
28372836
and @{ $here->{'_content'} } > $stack[0];
28382837

28392838
# make sure the index isn't too high
28402839
$here = $here->{'_content'}[ shift @stack ];
2841-
return if @stack and not ref $here;
2840+
return undef if @stack and not ref $here;
28422841

28432842
# we hit a text node when we expected a non-terminal element node
28442843
}
@@ -3078,7 +3077,6 @@ sub find_by_attribute {
30783077
return @matching;
30793078
}
30803079
else {
3081-
return unless @matching;
30823080
return $matching[0];
30833081
}
30843082
}
@@ -3653,7 +3651,6 @@ sub simplify_pres {
36533651

36543652
undef $sub;
36553653
return;
3656-
36573654
}
36583655

36593656
=method-second same_as
@@ -3970,24 +3967,24 @@ sub new_from_lol {
39703967

39713968
if (wantarray) {
39723969
my (@nodes) = map { ; ( ref($_) eq 'ARRAY' ) ? $sub->($_) : $_ } @_;
3973-
39743970
# Let text bits pass thru, I guess. This makes this act more like
39753971
# unshift_content et al. Undocumented.
3976-
undef $sub;
39773972

3973+
undef $sub;
39783974
# so it won't be in its own frame, so its refcount can hit 0
3975+
39793976
return @nodes;
39803977
}
39813978
else {
39823979
Carp::croak "new_from_lol in scalar context needs exactly one lol"
39833980
unless @_ == 1;
39843981
return $_[0] unless ref( $_[0] ) eq 'ARRAY';
3985-
39863982
# used to be a fatal error. still undocumented tho.
3983+
39873984
$node = $sub->( $_[0] );
39883985
undef $sub;
3989-
39903986
# so it won't be in its own frame, so its refcount can hit 0
3987+
39913988
return $node;
39923989
}
39933990
}
@@ -4099,7 +4096,7 @@ sub deobjectify_text {
40994096
}
41004097
}
41014098

4102-
return;
4099+
return undef;
41034100
}
41044101

41054102
=method-second number_lists

xt/author/zz_perlcritic.t

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ else {
1212
Test::Perl::Critic->import(
1313
-verbose => 8,
1414
-severity => 5,
15-
## This check fails to detect a package is modifying
16-
## objects of it's own class when passing objects in an array
17-
-exclude => ['ProhibitAccessOfPrivateData']
15+
-exclude => [
16+
# fails to detect a package is accessing objects of its own class:
17+
'ProhibitAccessOfPrivateData',
18+
# subs expected to return a scalar *should* "return undef":
19+
'ProhibitExplicitReturnUndef',
20+
],
1821
);
1922
}
2023

0 commit comments

Comments
 (0)