| 1 | package Venus::Vars; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use 5.018; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use Venus::Class 'attr', 'base', 'with'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | base 'Venus::Kind::Utility'; | |||||||||
| 11 | |||||||||||
| 12 | 1 | with 'Venus::Role::Valuable'; | |||||||||
| 13 | 1 | with 'Venus::Role::Buildable'; | |||||||||
| 14 | 1 | with 'Venus::Role::Accessible'; | |||||||||
| 15 | 1 | with 'Venus::Role::Proxyable'; | |||||||||
| 16 | |||||||||||
| 17 | # ATTRIBUTES | ||||||||||
| 18 | |||||||||||
| 19 | 1 | attr 'named'; | |||||||||
| 20 | |||||||||||
| 21 | # BUILDERS | ||||||||||
| 22 | |||||||||||
| 23 | sub build_proxy { | ||||||||||
| 24 | 2 | ▶ | my ($self, $package, $method, $value) = @_; | ||||||||
|
Subroutine
build_proxy: called CC=1 SCAR=0.2
Pod
undocumented
| |||||||||||
| 25 | |||||||||||
| 26 | 2 | my $has_value = exists $_[3]; | |||||||||
| 27 | |||||||||||
| 28 | return sub { | ||||||||||
| 29 | 2 | ▶ | return $self->get($method) if !$has_value; # no value | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=9.2
Branch: 1 branch
| |||||||||||
| 30 | 0 | return $self->set($method, $value); | |||||||||
| 31 | 2 | }; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | sub build_self { | ||||||||||
| 35 | 19 | ▶ | my ($self, $data) = @_; | ||||||||
|
Subroutine
build_self: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||
| 36 | |||||||||||
| 37 | 19 | ▶ | $self->named({}) if !$self->named; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | |||||||||||
| 39 | 19 | return $self; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | # METHODS | ||||||||||
| 43 | |||||||||||
| 44 | sub default { | ||||||||||
| 45 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
default: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 46 | |||||||||||
| 47 | 1 | return {%ENV}; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | sub exists { | ||||||||||
| 51 | 4 | ▶ | my ($self, $name) = @_; | ||||||||
|
Subroutine
exists: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 52 | |||||||||||
| 53 | 4 | ▶ | return if not defined $name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | |||||||||||
| 55 | 4 | my $pos = $self->name($name); | |||||||||
| 56 | |||||||||||
| 57 | 4 | ▶ | return if not defined $pos; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | |||||||||||
| 59 | 3 | return exists $self->value->{$pos}; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | sub get { | ||||||||||
| 63 | 6 | ▶ | my ($self, $name) = @_; | ||||||||
|
Subroutine
get: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 64 | |||||||||||
| 65 | 6 | ▶ | return if not defined $name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | |||||||||||
| 67 | 6 | my $pos = $self->name($name); | |||||||||
| 68 | |||||||||||
| 69 | 6 | ▶ | return if not defined $pos; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | |||||||||||
| 71 | 5 | return $self->value->{$pos}; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | sub name { | ||||||||||
| 75 | 18 | ▶ | my ($self, $name) = @_; | ||||||||
|
Subroutine
name: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 76 | |||||||||||
| 77 | 18 | ▶ | if (defined $self->named->{$name}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 6 | return $self->named->{$name}; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 12 | ▶ | if (defined $self->value->{$name}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 4 | return $name; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 8 | ▶ | if (defined $self->value->{uc($name)}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 4 | return uc($name); | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 4 | return undef; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | sub set { | ||||||||||
| 93 | 4 | ▶ | my ($self, $name, $data) = @_; | ||||||||
|
Subroutine
set: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 94 | |||||||||||
| 95 | 4 | ▶ | return if not defined $name; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | |||||||||||
| 97 | 4 | my $pos = $self->name($name); | |||||||||
| 98 | |||||||||||
| 99 | 4 | ▶ | return if not defined $pos; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | |||||||||||
| 101 | 3 | return $self->value->{$pos} = $data; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | sub unnamed { | ||||||||||
| 105 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
unnamed: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 106 | |||||||||||
| 107 | 1 | my $list = {}; | |||||||||
| 108 | |||||||||||
| 109 | 1 | my $vars = $self->value; | |||||||||
| 110 | 1 | my $data = +{reverse %{$self->named}}; | |||||||||
| 111 | |||||||||||
| 112 | 1 | for my $index (sort keys %$vars) { | |||||||||
| 113 | 2 | ▶ | unless (exists $data->{$index}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 1 | $list->{$index} = $vars->{$index}; | |||||||||
| 115 | } | ||||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | 1 | return $list; | |||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | 1 | 1; | |||||||||
| 122 | |||||||||||
| 123 | |||||||||||
| 124 | |||||||||||
| 125 | =head1 NAME | ||||||||||
| 126 | |||||||||||
| 127 | Venus::Vars - Vars Class | ||||||||||
| 128 | |||||||||||
| 129 | =cut | ||||||||||
| 130 | |||||||||||
| 131 | =head1 ABSTRACT | ||||||||||
| 132 | |||||||||||
| 133 | Vars Class for Perl 5 | ||||||||||
| 134 | |||||||||||
| 135 | =cut | ||||||||||
| 136 | |||||||||||
| 137 | =head1 SYNOPSIS | ||||||||||
| 138 | |||||||||||
| 139 | package main; | ||||||||||
| 140 | |||||||||||
| 141 | use Venus::Vars; | ||||||||||
| 142 | |||||||||||
| 143 | my $vars = Venus::Vars->new( | ||||||||||
| 144 | value => { USER => 'awncorp', HOME => '/home/awncorp', }, | ||||||||||
| 145 | named => { iam => 'USER', root => 'HOME', }, | ||||||||||
| 146 | ); | ||||||||||
| 147 | |||||||||||
| 148 | # $vars->root; # $ENV{HOME} | ||||||||||
| 149 | # $vars->home; # $ENV{HOME} | ||||||||||
| 150 | # $vars->get('home'); # $ENV{HOME} | ||||||||||
| 151 | # $vars->get('HOME'); # $ENV{HOME} | ||||||||||
| 152 | |||||||||||
| 153 | # $vars->iam; # $ENV{USER} | ||||||||||
| 154 | # $vars->user; # $ENV{USER} | ||||||||||
| 155 | # $vars->get('user'); # $ENV{USER} | ||||||||||
| 156 | # $vars->get('USER'); # $ENV{USER} | ||||||||||
| 157 | |||||||||||
| 158 | =cut | ||||||||||
| 159 | |||||||||||
| 160 | =head1 DESCRIPTION | ||||||||||
| 161 | |||||||||||
| 162 | This package provides methods for accessing C<%ENV> items. | ||||||||||
| 163 | |||||||||||
| 164 | =cut | ||||||||||
| 165 | |||||||||||
| 166 | =head1 ATTRIBUTES | ||||||||||
| 167 | |||||||||||
| 168 | This package has the following attributes: | ||||||||||
| 169 | |||||||||||
| 170 | =cut | ||||||||||
| 171 | |||||||||||
| 172 | =head2 named | ||||||||||
| 173 | |||||||||||
| 174 | named(HashRef) | ||||||||||
| 175 | |||||||||||
| 176 | This attribute is read-write, accepts C<(HashRef)> values, is optional, and defaults to C<{}>. | ||||||||||
| 177 | |||||||||||
| 178 | =cut | ||||||||||
| 179 | |||||||||||
| 180 | =head1 INHERITS | ||||||||||
| 181 | |||||||||||
| 182 | This package inherits behaviors from: | ||||||||||
| 183 | |||||||||||
| 184 | L<Venus::Kind::Utility> | ||||||||||
| 185 | |||||||||||
| 186 | =cut | ||||||||||
| 187 | |||||||||||
| 188 | =head1 INTEGRATES | ||||||||||
| 189 | |||||||||||
| 190 | This package integrates behaviors from: | ||||||||||
| 191 | |||||||||||
| 192 | L<Venus::Role::Accessible> | ||||||||||
| 193 | |||||||||||
| 194 | L<Venus::Role::Buildable> | ||||||||||
| 195 | |||||||||||
| 196 | L<Venus::Role::Proxyable> | ||||||||||
| 197 | |||||||||||
| 198 | L<Venus::Role::Valuable> | ||||||||||
| 199 | |||||||||||
| 200 | =cut | ||||||||||
| 201 | |||||||||||
| 202 | =head1 METHODS | ||||||||||
| 203 | |||||||||||
| 204 | This package provides the following methods: | ||||||||||
| 205 | |||||||||||
| 206 | =cut | ||||||||||
| 207 | |||||||||||
| 208 | =head2 default | ||||||||||
| 209 | |||||||||||
| 210 | default() (hashref) | ||||||||||
| 211 | |||||||||||
| 212 | The default method returns the default value, i.e. C<{%ENV}>. | ||||||||||
| 213 | |||||||||||
| 214 | I<Since C<0.01>> | ||||||||||
| 215 | |||||||||||
| 216 | =over 4 | ||||||||||
| 217 | |||||||||||
| 218 | =item default example 1 | ||||||||||
| 219 | |||||||||||
| 220 | # given: synopsis; | ||||||||||
| 221 | |||||||||||
| 222 | my $default = $vars->default; | ||||||||||
| 223 | |||||||||||
| 224 | # { USER => 'awncorp', HOME => '/home/awncorp', ... } | ||||||||||
| 225 | |||||||||||
| 226 | =back | ||||||||||
| 227 | |||||||||||
| 228 | =cut | ||||||||||
| 229 | |||||||||||
| 230 | =head2 exists | ||||||||||
| 231 | |||||||||||
| 232 | exists(string $key) (boolean) | ||||||||||
| 233 | |||||||||||
| 234 | The exists method takes a name or index and returns truthy if an associated | ||||||||||
| 235 | value exists. | ||||||||||
| 236 | |||||||||||
| 237 | I<Since C<0.01>> | ||||||||||
| 238 | |||||||||||
| 239 | =over 4 | ||||||||||
| 240 | |||||||||||
| 241 | =item exists example 1 | ||||||||||
| 242 | |||||||||||
| 243 | # given: synopsis; | ||||||||||
| 244 | |||||||||||
| 245 | my $exists = $vars->exists('iam');; | ||||||||||
| 246 | |||||||||||
| 247 | # 1 | ||||||||||
| 248 | |||||||||||
| 249 | =back | ||||||||||
| 250 | |||||||||||
| 251 | =over 4 | ||||||||||
| 252 | |||||||||||
| 253 | =item exists example 2 | ||||||||||
| 254 | |||||||||||
| 255 | # given: synopsis; | ||||||||||
| 256 | |||||||||||
| 257 | my $exists = $vars->exists('USER');; | ||||||||||
| 258 | |||||||||||
| 259 | # 1 | ||||||||||
| 260 | |||||||||||
| 261 | =back | ||||||||||
| 262 | |||||||||||
| 263 | =over 4 | ||||||||||
| 264 | |||||||||||
| 265 | =item exists example 3 | ||||||||||
| 266 | |||||||||||
| 267 | # given: synopsis; | ||||||||||
| 268 | |||||||||||
| 269 | my $exists = $vars->exists('PATH'); | ||||||||||
| 270 | |||||||||||
| 271 | # undef | ||||||||||
| 272 | |||||||||||
| 273 | =back | ||||||||||
| 274 | |||||||||||
| 275 | =over 4 | ||||||||||
| 276 | |||||||||||
| 277 | =item exists example 4 | ||||||||||
| 278 | |||||||||||
| 279 | # given: synopsis; | ||||||||||
| 280 | |||||||||||
| 281 | my $exists = $vars->exists('user'); | ||||||||||
| 282 | |||||||||||
| 283 | # 1 | ||||||||||
| 284 | |||||||||||
| 285 | =back | ||||||||||
| 286 | |||||||||||
| 287 | =cut | ||||||||||
| 288 | |||||||||||
| 289 | =head2 get | ||||||||||
| 290 | |||||||||||
| 291 | get(string $key) (any) | ||||||||||
| 292 | |||||||||||
| 293 | The get method takes a name or index and returns the associated value. | ||||||||||
| 294 | |||||||||||
| 295 | I<Since C<0.01>> | ||||||||||
| 296 | |||||||||||
| 297 | =over 4 | ||||||||||
| 298 | |||||||||||
| 299 | =item get example 1 | ||||||||||
| 300 | |||||||||||
| 301 | # given: synopsis; | ||||||||||
| 302 | |||||||||||
| 303 | my $get = $vars->get('iam'); | ||||||||||
| 304 | |||||||||||
| 305 | # "awncorp" | ||||||||||
| 306 | |||||||||||
| 307 | =back | ||||||||||
| 308 | |||||||||||
| 309 | =over 4 | ||||||||||
| 310 | |||||||||||
| 311 | =item get example 2 | ||||||||||
| 312 | |||||||||||
| 313 | # given: synopsis; | ||||||||||
| 314 | |||||||||||
| 315 | my $get = $vars->get('USER'); | ||||||||||
| 316 | |||||||||||
| 317 | # "awncorp" | ||||||||||
| 318 | |||||||||||
| 319 | =back | ||||||||||
| 320 | |||||||||||
| 321 | =over 4 | ||||||||||
| 322 | |||||||||||
| 323 | =item get example 3 | ||||||||||
| 324 | |||||||||||
| 325 | # given: synopsis; | ||||||||||
| 326 | |||||||||||
| 327 | my $get = $vars->get('PATH'); | ||||||||||
| 328 | |||||||||||
| 329 | # undef | ||||||||||
| 330 | |||||||||||
| 331 | =back | ||||||||||
| 332 | |||||||||||
| 333 | =over 4 | ||||||||||
| 334 | |||||||||||
| 335 | =item get example 4 | ||||||||||
| 336 | |||||||||||
| 337 | # given: synopsis; | ||||||||||
| 338 | |||||||||||
| 339 | my $get = $vars->get('user'); | ||||||||||
| 340 | |||||||||||
| 341 | # "awncorp" | ||||||||||
| 342 | |||||||||||
| 343 | =back | ||||||||||
| 344 | |||||||||||
| 345 | =cut | ||||||||||
| 346 | |||||||||||
| 347 | =head2 name | ||||||||||
| 348 | |||||||||||
| 349 | name(string $key) (string | undef) | ||||||||||
| 350 | |||||||||||
| 351 | The name method takes a name or index and returns index if the the associated | ||||||||||
| 352 | value exists. | ||||||||||
| 353 | |||||||||||
| 354 | I<Since C<0.01>> | ||||||||||
| 355 | |||||||||||
| 356 | =over 4 | ||||||||||
| 357 | |||||||||||
| 358 | =item name example 1 | ||||||||||
| 359 | |||||||||||
| 360 | # given: synopsis; | ||||||||||
| 361 | |||||||||||
| 362 | my $name = $vars->name('iam'); | ||||||||||
| 363 | |||||||||||
| 364 | # "USER" | ||||||||||
| 365 | |||||||||||
| 366 | =back | ||||||||||
| 367 | |||||||||||
| 368 | =over 4 | ||||||||||
| 369 | |||||||||||
| 370 | =item name example 2 | ||||||||||
| 371 | |||||||||||
| 372 | # given: synopsis; | ||||||||||
| 373 | |||||||||||
| 374 | my $name = $vars->name('USER'); | ||||||||||
| 375 | |||||||||||
| 376 | # "USER" | ||||||||||
| 377 | |||||||||||
| 378 | =back | ||||||||||
| 379 | |||||||||||
| 380 | =over 4 | ||||||||||
| 381 | |||||||||||
| 382 | =item name example 3 | ||||||||||
| 383 | |||||||||||
| 384 | # given: synopsis; | ||||||||||
| 385 | |||||||||||
| 386 | my $name = $vars->name('PATH'); | ||||||||||
| 387 | |||||||||||
| 388 | # undef | ||||||||||
| 389 | |||||||||||
| 390 | =back | ||||||||||
| 391 | |||||||||||
| 392 | =over 4 | ||||||||||
| 393 | |||||||||||
| 394 | =item name example 4 | ||||||||||
| 395 | |||||||||||
| 396 | # given: synopsis; | ||||||||||
| 397 | |||||||||||
| 398 | my $name = $vars->name('user'); | ||||||||||
| 399 | |||||||||||
| 400 | # "USER" | ||||||||||
| 401 | |||||||||||
| 402 | =back | ||||||||||
| 403 | |||||||||||
| 404 | =cut | ||||||||||
| 405 | |||||||||||
| 406 | =head2 set | ||||||||||
| 407 | |||||||||||
| 408 | set(string $key, any $value) (any) | ||||||||||
| 409 | |||||||||||
| 410 | The set method takes a name or index and sets the value provided if the | ||||||||||
| 411 | associated argument exists. | ||||||||||
| 412 | |||||||||||
| 413 | I<Since C<0.01>> | ||||||||||
| 414 | |||||||||||
| 415 | =over 4 | ||||||||||
| 416 | |||||||||||
| 417 | =item set example 1 | ||||||||||
| 418 | |||||||||||
| 419 | # given: synopsis; | ||||||||||
| 420 | |||||||||||
| 421 | my $set = $vars->set('iam', 'root'); | ||||||||||
| 422 | |||||||||||
| 423 | # "root" | ||||||||||
| 424 | |||||||||||
| 425 | =back | ||||||||||
| 426 | |||||||||||
| 427 | =over 4 | ||||||||||
| 428 | |||||||||||
| 429 | =item set example 2 | ||||||||||
| 430 | |||||||||||
| 431 | # given: synopsis; | ||||||||||
| 432 | |||||||||||
| 433 | my $set = $vars->set('USER', 'root'); | ||||||||||
| 434 | |||||||||||
| 435 | # "root" | ||||||||||
| 436 | |||||||||||
| 437 | =back | ||||||||||
| 438 | |||||||||||
| 439 | =over 4 | ||||||||||
| 440 | |||||||||||
| 441 | =item set example 3 | ||||||||||
| 442 | |||||||||||
| 443 | # given: synopsis; | ||||||||||
| 444 | |||||||||||
| 445 | my $set = $vars->set('PATH', '/tmp'); | ||||||||||
| 446 | |||||||||||
| 447 | # undef | ||||||||||
| 448 | |||||||||||
| 449 | =back | ||||||||||
| 450 | |||||||||||
| 451 | =over 4 | ||||||||||
| 452 | |||||||||||
| 453 | =item set example 4 | ||||||||||
| 454 | |||||||||||
| 455 | # given: synopsis; | ||||||||||
| 456 | |||||||||||
| 457 | my $set = $vars->set('user', 'root'); | ||||||||||
| 458 | |||||||||||
| 459 | # "root" | ||||||||||
| 460 | |||||||||||
| 461 | =back | ||||||||||
| 462 | |||||||||||
| 463 | =cut | ||||||||||
| 464 | |||||||||||
| 465 | =head2 unnamed | ||||||||||
| 466 | |||||||||||
| 467 | unnamed() (hashref) | ||||||||||
| 468 | |||||||||||
| 469 | The unnamed method returns an arrayref of values which have not been named | ||||||||||
| 470 | using the C<named> attribute. | ||||||||||
| 471 | |||||||||||
| 472 | I<Since C<0.01>> | ||||||||||
| 473 | |||||||||||
| 474 | =over 4 | ||||||||||
| 475 | |||||||||||
| 476 | =item unnamed example 1 | ||||||||||
| 477 | |||||||||||
| 478 | package main; | ||||||||||
| 479 | |||||||||||
| 480 | use Venus::Vars; | ||||||||||
| 481 | |||||||||||
| 482 | my $vars = Venus::Vars->new( | ||||||||||
| 483 | value => { USER => 'awncorp', HOME => '/home/awncorp', }, | ||||||||||
| 484 | named => { root => 'HOME', }, | ||||||||||
| 485 | ); | ||||||||||
| 486 | |||||||||||
| 487 | my $unnamed = $vars->unnamed; | ||||||||||
| 488 | |||||||||||
| 489 | # { USER => "awncorp" } | ||||||||||
| 490 | |||||||||||
| 491 | =back | ||||||||||
| 492 | |||||||||||
| 493 | =cut | ||||||||||
| 494 | |||||||||||
| 495 | =head1 AUTHORS | ||||||||||
| 496 | |||||||||||
| 497 | Awncorp, C<awncorp@cpan.org> | ||||||||||
| 498 | |||||||||||
| 499 | =cut | ||||||||||
| 500 | |||||||||||
| 501 | =head1 LICENSE | ||||||||||
| 502 | |||||||||||
| 503 | Copyright (C) 2000, Awncorp, C<awncorp@cpan.org>. | ||||||||||
| 504 | |||||||||||
| 505 | This program is free software, you can redistribute it and/or modify it under | ||||||||||
| 506 | the terms of the Apache license version 2.0. | ||||||||||
| 507 | |||||||||||
| 508 | =cut | ||||||||||