| 1 | package vars::i; | ||||||||||||||||||||||
| 2 | 5 | ▶ | use 5.006001; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 3 | |||||||||||||||||||||||
| 4 | 5 | our $VERSION = '2.000000'; # Prerelease leading to v2.0.0 | |||||||||||||||||||||
| 5 | |||||||||||||||||||||||
| 6 | 5 | ▶ | use strict qw(vars subs); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 5 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | # Turn a scalar, arrayref, or hashref into a list | ||||||||||||||||||||||
| 10 | 11 | ▶ | sub _unpack { | ||||||||||||||||||||
|
Subroutine
_unpack: called CC=3 SCAR=11.0
| |||||||||||||||||||||||
| 11 | 11 | ▶ | if( ref $_[0] eq 'ARRAY' ){ | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 12 | 7 | return @{$_[0]}; | |||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||
| 14 | elsif( ref $_[0] eq 'HASH' ){ | ||||||||||||||||||||||
| 15 | 3 | return %{$_[0]}; | |||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||
| 17 | else { | ||||||||||||||||||||||
| 18 | 1 | return ($_[0]); | |||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||
| 20 | } #_unpack | ||||||||||||||||||||||
| 21 | |||||||||||||||||||||||
| 22 | 42 | ▶ | sub import { | ||||||||||||||||||||
|
Subroutine
import: called CC=20 SCAR=30.0
| |||||||||||||||||||||||
| 23 | 42 | ▶ | return if @_ < 2; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 24 | 37 | my( $pack, $first_var, @value ) = @_; | |||||||||||||||||||||
| 25 | 37 | my $callpack = caller; | |||||||||||||||||||||
| 26 | |||||||||||||||||||||||
| 27 | 37 | my %definitions; | |||||||||||||||||||||
| 28 | |||||||||||||||||||||||
| 29 | 37 | ▶ | if( not @value ){ | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 30 | 9 | ▶ | if( ref $first_var eq 'ARRAY' ){ # E.g., use vars [ foo=>, bar=>... ]; | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 31 | 6 | %definitions = @$first_var; | |||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||
| 33 | elsif( ref $first_var eq 'HASH' ){ # E.g., use vars { foo=>, bar=>... }; | ||||||||||||||||||||||
| 34 | 2 | %definitions = %$first_var; | |||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||
| 36 | else { | ||||||||||||||||||||||
| 37 | 1 | return; # No value given --- no-op; not an error. | |||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||
| 40 | elsif(@value == 1) { # E.g., use vars foo => <str/hashref/arrayref> | ||||||||||||||||||||||
| 41 | 26 | %definitions = ( $first_var => $value[0] ); | |||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||
| 43 | else { | ||||||||||||||||||||||
| 44 | 2 | %definitions = ( $first_var => [@value] ); | |||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||
| 46 | |||||||||||||||||||||||
| 47 | #require Data::Dumper; # For debugging | ||||||||||||||||||||||
| 48 | #print Data::Dumper->Dump([\%definitions], ['definitions']); | ||||||||||||||||||||||
| 49 | |||||||||||||||||||||||
| 50 | 36 | ▶ | while( my ($var, $val) = each %definitions ){ | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 51 | |||||||||||||||||||||||
| 52 | 42 | ▶ | if( my( $ch, $sym ) = $var =~ /^([-\$\@\%\*\&])(.+)$/ ){ | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 53 | 40 | ▶ | if( $ch eq '-' ){ # An option | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 54 | 2 | require Carp; | |||||||||||||||||||||
| 55 | 2 | Carp::croak('vars::i does not yet support any options!'); | |||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||
| 57 | ▶ | elsif( $sym !~ /^(\w+(::|'))+\w+$/ && $sym =~ /\W|(^\d+$)/ ){ | |||||||||||||||||||||
|
Condition100%3/3
not $sym =~ /^(\w+(::|'))+\w+$/ and $sym =~ /\W|(^\d+$)/
MC/DC100%2/2
not $sym =~ /^(\w+(::|'))+\w+$/ and $sym =~ /\W|(^\d+$)/
not $sym =~ /^(\w+(::|'))+\w+$/
$sym =~ /\W|(^\d+$)/
Truth table100%3/3
not(A) and B
A= not $sym =~ /^(\w+(::|'))+\w+$/B= $sym =~ /\W|(^\d+$)/
| |||||||||||||||||||||||
| 58 | # ^^ Skip fully-qualified names ^^ Check special names | ||||||||||||||||||||||
| 59 | |||||||||||||||||||||||
| 60 | # A variable name we can't or won't handle | ||||||||||||||||||||||
| 61 | 9 | require Carp; | |||||||||||||||||||||
| 62 | |||||||||||||||||||||||
| 63 | 9 | ▶ | if( $sym =~ /^\w+[[{].*[]}]$/ ){ | ||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||
| 64 | 2 | Carp::croak("Can't declare individual elements of hash or array"); | |||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||
| 66 | elsif( $sym =~ /^(\d+|\W|\^[\[\]A-Z\^_\?]|\{\^[a-zA-Z0-9]+\})$/ ){ | ||||||||||||||||||||||
| 67 | 5 | Carp::croak("Refusing to initialize special variable $ch$sym"); | |||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||
| 69 | else { | ||||||||||||||||||||||
| 70 | 2 | Carp::croak("I can't recognize $ch$sym as a variable name"); | |||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||
| 73 | |||||||||||||||||||||||
| 74 | 29 | ▶ | $sym = "${callpack}::$sym" unless $sym =~ /::/; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 75 | |||||||||||||||||||||||
| 76 | 29 | ▶ | if( $ch eq '$' ){ | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 77 | 14 | *{$sym} = \$$sym; | |||||||||||||||||||||
| 78 | 14 | ${$sym} = $val; | |||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||
| 80 | elsif( $ch eq '@' ){ | ||||||||||||||||||||||
| 81 | 7 | *{$sym} = \@$sym; | |||||||||||||||||||||
| 82 | 7 | @{$sym} = _unpack $val; | |||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||
| 84 | elsif( $ch eq '%' ){ | ||||||||||||||||||||||
| 85 | 4 | *{$sym} = \%$sym; | |||||||||||||||||||||
| 86 | 4 | %{$sym} = _unpack $val; | |||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||
| 88 | elsif( $ch eq '*' ){ | ||||||||||||||||||||||
| 89 | 1 | *{$sym} = \*$sym; | |||||||||||||||||||||
| 90 | 1 | (*{$sym}) = $val; | |||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||
| 92 | else { # $ch eq '&'; guaranteed by the regex above. | ||||||||||||||||||||||
| 93 | 3 | my ($param) = $val; | |||||||||||||||||||||
| 94 | 3 | ▶ | if(ref $param) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 95 | # NOTE: for now, permit any ref, since we can't determine | ||||||||||||||||||||||
| 96 | # refs overload &{}. If necessary, we can later use | ||||||||||||||||||||||
| 97 | # Scalar::Util 1.25+'s blessed(), and allow CODE refs | ||||||||||||||||||||||
| 98 | # or blessed refs. | ||||||||||||||||||||||
| 99 | 1 | *{$sym} = $param; | |||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||
| 101 | else { | ||||||||||||||||||||||
| 102 | 2 | require Carp; | |||||||||||||||||||||
| 103 | 2 | ▶ | Carp::croak("Can't assign non-reference " . | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 104 | (defined($param) ? $param : '<undef>') . | ||||||||||||||||||||||
| 105 | " to $sym"); | ||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||
| 108 | # There is no else, because the regex above guarantees | ||||||||||||||||||||||
| 109 | # that $ch has one of the values we tested. | ||||||||||||||||||||||
| 110 | |||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||
| 112 | else { # Name didn't match the regex above | ||||||||||||||||||||||
| 113 | 2 | require Carp; | |||||||||||||||||||||
| 114 | 2 | Carp::croak("'$var' is not a valid variable or option name"); | |||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||
| 117 | } #import() | ||||||||||||||||||||||
| 118 | |||||||||||||||||||||||
| 119 | 5 | 1; | |||||||||||||||||||||
| 120 | __END__ | ||||||||||||||||||||||