File Coverage

blib/lib/Perl/Critic/Policy/BuiltinFunctions/ProhibitSleepViaSelect.pm
Criterion Covered Total %
statement 26 34 76.4
branch 1 10 10.0
condition n/a
subroutine 12 13 92.3
pod 4 5 80.0
total 43 62 69.3


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::BuiltinFunctions::ProhibitSleepViaSelect;
2              
3 40     40   25763 use 5.010001;
  40         219  
4 40     40   298 use strict;
  40         125  
  40         874  
5 40     40   303 use warnings;
  40         183  
  40         1168  
6 40     40   257 use Readonly;
  40         144  
  40         2250  
7              
8 40     40   358 use List::SomeUtils qw(any);
  40         138  
  40         2127  
9 40     40   393 use Perl::Critic::Utils qw{ :severities :classification :ppi };
  40         154  
  40         2214  
10 40     40   15093 use parent 'Perl::Critic::Policy';
  40         133  
  40         291  
11              
12             our $VERSION = '1.150';
13              
14             #-----------------------------------------------------------------------------
15              
16             Readonly::Scalar my $DESC => q{"select" used to emulate "sleep"};
17             Readonly::Scalar my $EXPL => [168];
18             Readonly::Scalar my $SELECT_ARGUMENT_COUNT => 4;
19              
20             #-----------------------------------------------------------------------------
21              
22 89     89 0 1679 sub supported_parameters { return () }
23 74     74 1 324 sub default_severity { return $SEVERITY_HIGHEST }
24 92     92 1 343 sub default_themes { return qw( core pbp bugs ) }
25 36     36 1 92 sub applies_to { return 'PPI::Token::Word' }
26              
27             #-----------------------------------------------------------------------------
28              
29             sub violates {
30 358     358 1 601 my ($self, $elem, undef) = @_;
31              
32 358 50       599 return if $elem->content() ne 'select';
33 0 0         return if ! is_function_call($elem);
34              
35 0           my @arguments = parse_arg_list($elem);
36 0 0         return if $SELECT_ARGUMENT_COUNT != @arguments;
37              
38 0 0   0     return if any { $_->[0] ne 'undef' } @arguments[0..2];
  0            
39              
40 0 0         if ( $arguments[-1]->[0] ne 'undef' ) {
41 0           return $self->violation( $DESC, $EXPL, $elem );
42             }
43              
44 0           return; #ok!
45             }
46              
47             1;
48              
49             __END__
50              
51             #-----------------------------------------------------------------------------
52              
53             =pod
54              
55             =for stopwords perlfunc
56              
57             =head1 NAME
58              
59             Perl::Critic::Policy::BuiltinFunctions::ProhibitSleepViaSelect - Use L<Time::HiRes|Time::HiRes> instead of something like C<select(undef, undef, undef, .05)>.
60              
61              
62             =head1 AFFILIATION
63              
64             This Policy is part of the core L<Perl::Critic|Perl::Critic>
65             distribution.
66              
67              
68             =head1 DESCRIPTION
69              
70             Conway discourages the use of C<select()> for performing non-integer
71             sleeps. Although documented in L<perlfunc|perlfunc>, it's something
72             that generally requires the reader to read C<perldoc -f select> to
73             figure out what it should be doing. Instead, Conway recommends that
74             you use the C<Time::HiRes> module when you want to sleep.
75              
76             select undef, undef, undef, 0.25; # not ok
77              
78             use Time::HiRes;
79             sleep( 0.25 ); # ok
80              
81              
82             =head1 CONFIGURATION
83              
84             This Policy is not configurable except for the standard options.
85              
86              
87             =head1 SEE ALSO
88              
89             L<Time::HiRes|Time::HiRes>.
90              
91              
92             =head1 AUTHOR
93              
94             Graham TerMarsch <graham@howlingfrog.com>
95              
96              
97             =head1 COPYRIGHT
98              
99             Copyright (c) 2005-2011 Graham TerMarsch. All rights reserved.
100              
101             This program is free software; you can redistribute it and/or modify
102             it under the same terms as Perl itself.
103              
104             =cut
105              
106             # Local Variables:
107             # mode: cperl
108             # cperl-indent-level: 4
109             # fill-column: 78
110             # indent-tabs-mode: nil
111             # c-indentation-style: bsd
112             # End:
113             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :