/usr/share/perl5/Perl/Critic/UserProfile.pm is in libperl-critic-perl 1.122-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 | package Perl::Critic::UserProfile;
use 5.006001;
use strict;
use warnings;
use English qw(-no_match_vars);
use Readonly;
use Config::Tiny qw();
use File::Spec qw();
use Perl::Critic::OptionsProcessor qw();
use Perl::Critic::Utils qw{ :characters policy_long_name policy_short_name };
use Perl::Critic::Exception::Fatal::Internal qw{ throw_internal };
use Perl::Critic::Exception::Configuration::Generic qw{ throw_generic };
use Perl::Critic::PolicyConfig;
our $VERSION = '1.122';
#-----------------------------------------------------------------------------
sub new {
my ( $class, %args ) = @_;
my $self = bless {}, $class;
$self->_init( %args );
return $self;
}
#-----------------------------------------------------------------------------
sub _init {
my ( $self, %args ) = @_;
# The profile can be defined, undefined, or an empty string.
my $profile = defined $args{-profile} ? $args{-profile} : _find_profile_path();
$self->_load_profile( $profile );
$self->_set_options_processor();
return $self;
}
#-----------------------------------------------------------------------------
sub options_processor {
my ($self) = @_;
return $self->{_options_processor};
}
#-----------------------------------------------------------------------------
sub policy_params {
my ( $self, $policy ) = @_;
my $short_name = policy_short_name($policy);
return Perl::Critic::PolicyConfig->new(
$short_name,
$self->raw_policy_params($policy),
);
}
#-----------------------------------------------------------------------------
sub raw_policy_params {
my ( $self, $policy ) = @_;
my $profile = $self->{_profile};
my $long_name = ref $policy || policy_long_name( $policy );
my $short_name = policy_short_name( $long_name );
return
$profile->{$short_name}
|| $profile->{$long_name}
|| $profile->{"-$short_name"}
|| $profile->{"-$long_name"}
|| {};
}
#-----------------------------------------------------------------------------
sub policy_is_disabled {
my ( $self, $policy ) = @_;
my $profile = $self->{_profile};
my $long_name = ref $policy || policy_long_name( $policy );
my $short_name = policy_short_name( $long_name );
return exists $profile->{"-$short_name"}
|| exists $profile->{"-$long_name"};
}
#-----------------------------------------------------------------------------
sub policy_is_enabled {
my ( $self, $policy ) = @_;
my $profile = $self->{_profile};
my $long_name = ref $policy || policy_long_name( $policy );
my $short_name = policy_short_name( $long_name );
return exists $profile->{$short_name}
|| exists $profile->{$long_name};
}
#-----------------------------------------------------------------------------
sub listed_policies {
my ( $self, $policy ) = @_;
my @normalized_policy_names = ();
for my $policy_name ( sort keys %{$self->{_profile}} ) {
$policy_name =~ s/\A - //xmso; #Chomp leading "-"
my $policy_long_name = policy_long_name( $policy_name );
push @normalized_policy_names, $policy_long_name;
}
return @normalized_policy_names;
}
#-----------------------------------------------------------------------------
sub source {
my ( $self ) = @_;
return $self->{_source};
}
sub _set_source {
my ( $self, $source ) = @_;
$self->{_source} = $source;
return;
}
#-----------------------------------------------------------------------------
# Begin PRIVATE methods
Readonly::Hash my %LOADER_FOR => (
ARRAY => \&_load_profile_from_array,
DEFAULT => \&_load_profile_from_file,
HASH => \&_load_profile_from_hash,
SCALAR => \&_load_profile_from_string,
);
sub _load_profile {
my ( $self, $profile ) = @_;
my $ref_type = ref $profile || 'DEFAULT';
my $loader = $LOADER_FOR{$ref_type};
if (not $loader) {
throw_internal qq{Can't load UserProfile from type "$ref_type"};
}
$self->{_profile} = $loader->($self, $profile);
return $self;
}
#-----------------------------------------------------------------------------
sub _set_options_processor {
my ($self) = @_;
my $profile = $self->{_profile};
my $defaults = delete $profile->{__defaults__} || {};
$self->{_options_processor} =
Perl::Critic::OptionsProcessor->new( %{ $defaults } );
return $self;
}
#-----------------------------------------------------------------------------
sub _load_profile_from_file {
my ( $self, $file ) = @_;
# Handle special cases.
return {} if not defined $file;
return {} if $file eq $EMPTY;
return {} if $file eq 'NONE';
$self->_set_source( $file );
my $profile = Config::Tiny->read( $file );
if (not defined $profile) {
my $errstr = Config::Tiny::errstr();
throw_generic
message => qq{Could not parse profile "$file": $errstr},
source => $file;
}
_fix_defaults_key( $profile );
return $profile;
}
#-----------------------------------------------------------------------------
sub _load_profile_from_array {
my ( $self, $array_ref ) = @_;
my $joined = join qq{\n}, @{ $array_ref };
my $profile = Config::Tiny->read_string( $joined );
if (not defined $profile) {
throw_generic 'Profile error: ' . Config::Tiny::errstr();
}
_fix_defaults_key( $profile );
return $profile;
}
#-----------------------------------------------------------------------------
sub _load_profile_from_string {
my ( $self, $string ) = @_;
my $profile = Config::Tiny->read_string( ${ $string } );
if (not defined $profile) {
throw_generic 'Profile error: ' . Config::Tiny::errstr();
}
_fix_defaults_key( $profile );
return $profile;
}
#-----------------------------------------------------------------------------
sub _load_profile_from_hash {
my ( $self, $hash_ref ) = @_;
return $hash_ref;
}
#-----------------------------------------------------------------------------
sub _find_profile_path {
#Define default filename
my $rc_file = '.perlcriticrc';
#Check explicit environment setting
return $ENV{PERLCRITIC} if exists $ENV{PERLCRITIC};
#Check current directory
return $rc_file if -f $rc_file;
#Check home directory
if ( my $home_dir = _find_home_dir() ) {
my $path = File::Spec->catfile( $home_dir, $rc_file );
return $path if -f $path;
}
#No profile defined
return;
}
#-----------------------------------------------------------------------------
sub _find_home_dir {
# Try using File::HomeDir
if ( eval { require File::HomeDir } ) {
return File::HomeDir->my_home();
}
# Check usual environment vars
for my $key (qw(HOME USERPROFILE HOMESHARE)) {
next if not defined $ENV{$key};
return $ENV{$key} if -d $ENV{$key};
}
# No home directory defined
return;
}
#-----------------------------------------------------------------------------
# !$%@$%^ Config::Tiny uses a completely non-descriptive name for global
# values.
sub _fix_defaults_key {
my ( $profile ) = @_;
my $defaults = delete $profile->{_};
if ($defaults) {
$profile->{__defaults__} = $defaults;
}
return;
}
1;
__END__
#-----------------------------------------------------------------------------
=pod
=for stopwords UserProfile
=head1 NAME
Perl::Critic::UserProfile - The contents of the user's profile, often F<.perlcriticrc>.
=head1 DESCRIPTION
This is a helper class that encapsulates the contents of the user's
profile, which is usually stored in a F<.perlcriticrc> file. There are
no user-serviceable parts here.
=head1 INTERFACE SUPPORT
This is considered to be a non-public class. Its interface is subject
to change without notice.
=head1 CONSTRUCTOR
=over
=item C< new( -profile => $p ) >
B<-profile> is the path to the user's profile. If -profile is not
defined, then it looks for the profile at F<./.perlcriticrc> and then
F<$HOME/.perlcriticrc>. If neither of those files exists, then the
UserProfile is created with default values.
This object does not take into account any command-line overrides;
L<Perl::Critic::Config|Perl::Critic::Config> does that.
=back
=head1 METHODS
=over
=item C< options_processor() >
Returns the
L<Perl::Critic::OptionsProcessor|Perl::Critic::OptionsProcessor>
object for this UserProfile.
=item C< policy_is_disabled( $policy ) >
Given a reference to a L<Perl::Critic::Policy|Perl::Critic::Policy>
object or the name of one, returns true if the user has disabled that
policy in their profile.
=item C< policy_is_enabled( $policy ) >
Given a reference to a L<Perl::Critic::Policy|Perl::Critic::Policy>
object or the name of one, returns true if the user has explicitly
enabled that policy in their user profile.
=item C< policy_params( $policy ) >
Given a reference to a L<Perl::Critic::Policy|Perl::Critic::Policy>
object or the name of one, returns a
L<Perl::Critic::PolicyConfig|Perl::Critic::PolicyConfig> for the
user's configuration parameters for that policy.
=item C< raw_policy_params( $policy ) >
Given a reference to a L<Perl::Critic::Policy|Perl::Critic::Policy>
object or the name of one, returns a reference to a hash of the user's
configuration parameters for that policy.
=item C< listed_policies() >
Returns a list of the names of all the Policies that are mentioned in
the profile. The Policy names will be fully qualified (e.g.
Perl::Critic::Foo).
=item C< source() >
The place where the profile information came from, if available.
Usually the path to a F<.perlcriticrc>.
=back
=head1 SEE ALSO
L<Perl::Critic::Config|Perl::Critic::Config>,
L<Perl::Critic::OptionsProcessor|Perl::Critic::OptionsProcessor>
=head1 AUTHOR
Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
=head1 COPYRIGHT
Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself. The full text of this license
can be found in the LICENSE file included with this module.
=cut
# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# fill-column: 78
# indent-tabs-mode: nil
# c-indentation-style: bsd
# End:
# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :
|