← Index
NYTProf Performance Profile   « line view »
For /Users/timbo/perl5/perlbrew/perls/perl-5.18.2/bin/perlcritic
  Run on Sat Mar 19 22:12:22 2016
Reported on Sat Mar 19 22:14:13 2016

Filename/Users/timbo/perl5/perlbrew/perls/perl-5.18.2/lib/site_perl/5.18.2/Config/Tiny.pm
StatementsExecuted 37 statements in 820µs
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
11178µs82µsConfig::Tiny::::read_stringConfig::Tiny::read_string
11128µs144µsConfig::Tiny::::readConfig::Tiny::read
11118µs18µsConfig::Tiny::::CORE:openConfig::Tiny::CORE:open (opcode)
11112µs23µsConfig::Tiny::::BEGIN@5Config::Tiny::BEGIN@5
11111µs11µsConfig::Tiny::::CORE:readlineConfig::Tiny::CORE:readline (opcode)
11110µs10µsConfig::Tiny::::BEGIN@7Config::Tiny::BEGIN@7
1116µs6µsConfig::Tiny::::CORE:closeConfig::Tiny::CORE:close (opcode)
7114µs4µsConfig::Tiny::::CORE:matchConfig::Tiny::CORE:match (opcode)
0000s0sConfig::Tiny::::_errorConfig::Tiny::_error
0000s0sConfig::Tiny::::errstrConfig::Tiny::errstr
0000s0sConfig::Tiny::::newConfig::Tiny::new
0000s0sConfig::Tiny::::writeConfig::Tiny::write
0000s0sConfig::Tiny::::write_stringConfig::Tiny::write_string
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package Config::Tiny;
2
3# If you thought Config::Simple was small...
4
5248µs235µs
# spent 23µs (12+12) within Config::Tiny::BEGIN@5 which was called: # once (12µs+12µs) by Perl::Critic::UserProfile::BEGIN@17 at line 5
use strict;
# spent 23µs making 1 call to Config::Tiny::BEGIN@5 # spent 12µs making 1 call to strict::import
61600nsour $VERSION = '2.20'; # Also change version # in t/02.main.t.
7
# spent 10µs within Config::Tiny::BEGIN@7 which was called: # once (10µs+0s) by Perl::Critic::UserProfile::BEGIN@17 at line 10
BEGIN {
816µs require 5.008001;
914µs $Config::Tiny::errstr = '';
101616µs110µs}
# spent 10µs making 1 call to Config::Tiny::BEGIN@7
11
12# Create an empty object
13sub new { bless {}, shift }
14
15# Create an object from a file
16
# spent 144µs (28+116) within Config::Tiny::read which was called: # once (28µs+116µs) by Perl::Critic::UserProfile::_load_profile_from_file at line 196 of Perl/Critic/UserProfile.pm
sub read {
171900ns my $class = ref $_[0] ? ref shift : shift;
181500ns my $file = shift or return $class->_error('No file name provided');
19
20 # Slurp in the file.
21
221100ns my $encoding = shift;
231500ns $encoding = $encoding ? "<:$encoding" : '<';
2412µs local $/ = undef;
25
26124µs118µs open( CFG, $encoding, $file ) or return $class->_error( "Failed to open file '$file' for reading: $!" );
# spent 18µs making 1 call to Config::Tiny::CORE:open
27116µs111µs my $contents = <CFG>;
# spent 11µs making 1 call to Config::Tiny::CORE:readline
28110µs16µs close( CFG );
# spent 6µs making 1 call to Config::Tiny::CORE:close
29
301400ns return $class -> _error("Reading from '$file' returned undef") if (! defined $contents);
31
3217µs182µs return $class->read_string( $contents );
# spent 82µs making 1 call to Config::Tiny::read_string
33}
34
35# Create an object from a string
36
# spent 82µs (78+4) within Config::Tiny::read_string which was called: # once (78µs+4µs) by Config::Tiny::read at line 32
sub read_string {
371800ns my $class = ref $_[0] ? ref shift : shift;
3812µs my $self = bless {}, $class;
391300ns return undef unless defined $_[0];
40
41 # Parse the file
421500ns my $ns = '_';
431200ns my $counter = 0;
44157µs foreach ( split /(?:\015{1,2}\012|\015|\012)/, shift ) {
457300ns $counter++;
46
47 # Skip comments and empty lines
48717µs74µs next if /^\s*(?:\#|\;|$)/;
# spent 4µs making 7 calls to Config::Tiny::CORE:match, avg 586ns/call
49
50 # Remove inline comments
51 s/\s\;\s.+$//g;
52
53 # Handle section headers
54 if ( /^\s*\[\s*(.+?)\s*\]\s*$/ ) {
55 # Create the sub-hash if it doesn't exist.
56 # Without this sections without keys will not
57 # appear at all in the completed struct.
58 $self->{$ns = $1} ||= {};
59 next;
60 }
61
62 # Handle properties
63 if ( /^\s*([^=]+?)\s*=\s*(.*?)\s*$/ ) {
64 $self->{$ns}->{$1} = $2;
65 next;
66 }
67
68 return $self->_error( "Syntax error at line $counter: '$_'" );
69 }
70
7113µs $self;
72}
73
74# Save an object to a file
75sub write {
76 my $self = shift;
77 my $file = shift or return $self->_error('No file name provided');
78 my $encoding = shift;
79 $encoding = $encoding ? ">:$encoding" : '>';
80
81 # Write it to the file
82 my $string = $self->write_string;
83 return undef unless defined $string;
84 open( CFG, $encoding, $file ) or return $self->_error(
85 "Failed to open file '$file' for writing: $!"
86 );
87 print CFG $string;
88 close CFG;
89
90 return 1;
91}
92
93# Save an object to a string
94sub write_string {
95 my $self = shift;
96
97 my $contents = '';
98 foreach my $section ( sort { (($b eq '_') <=> ($a eq '_')) || ($a cmp $b) } keys %$self ) {
99 # Check for several known-bad situations with the section
100 # 1. Leading whitespace
101 # 2. Trailing whitespace
102 # 3. Newlines in section name
103 return $self->_error(
104 "Illegal whitespace in section name '$section'"
105 ) if $section =~ /(?:^\s|\n|\s$)/s;
106 my $block = $self->{$section};
107 $contents .= "\n" if length $contents;
108 $contents .= "[$section]\n" unless $section eq '_';
109 foreach my $property ( sort keys %$block ) {
110 return $self->_error(
111 "Illegal newlines in property '$section.$property'"
112 ) if $block->{$property} =~ /(?:\012|\015)/s;
113 $contents .= "$property=$block->{$property}\n";
114 }
115 }
116
117 $contents;
118}
119
120# Error handling
121sub errstr { $Config::Tiny::errstr }
122sub _error { $Config::Tiny::errstr = $_[1]; undef }
123
12412µs1;
125
126__END__
 
# spent 6µs within Config::Tiny::CORE:close which was called: # once (6µs+0s) by Config::Tiny::read at line 28
sub Config::Tiny::CORE:close; # opcode
# spent 4µs within Config::Tiny::CORE:match which was called 7 times, avg 586ns/call: # 7 times (4µs+0s) by Config::Tiny::read_string at line 48, avg 586ns/call
sub Config::Tiny::CORE:match; # opcode
# spent 18µs within Config::Tiny::CORE:open which was called: # once (18µs+0s) by Config::Tiny::read at line 26
sub Config::Tiny::CORE:open; # opcode
# spent 11µs within Config::Tiny::CORE:readline which was called: # once (11µs+0s) by Config::Tiny::read at line 27
sub Config::Tiny::CORE:readline; # opcode