| 1 | # [[[ HEADER ]]] | ||
| 2 | package Perl::Type::String; | ||
| 3 | 5 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 5 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 5 | ▶ | use Perl::Config; # don't use Perl::Types inside itself, in order to avoid circular includes |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | our $VERSION = 0.015_000; | ||
| 7 | |||
| 8 | # [[[ OO INHERITANCE ]]] | ||
| 9 | 5 | ▶ | use parent qw(Perl::Type::Scalar); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 5 | ▶ | use Perl::Type::Scalar; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | # [[[ CRITICS ]]] | ||
| 13 | ## no critic qw(RequireInterpolationOfMetachars) # USER DEFAULT 2: allow single-quoted control characters & sigils | ||
| 14 | ## no critic qw(Capitalization ProhibitMultiplePackages ProhibitReusedNames) # SYSTEM DEFAULT 3: allow multiple & lower case package names | ||
| 15 | |||
| 16 | # [[[ SUB-TYPES ]]] | ||
| 17 | # a string is 0 or more letters, digits, or other ASCII (Unicode???) symbols | ||
| 18 | package string; | ||
| 19 | 5 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 5 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 5 | ▶ | use parent qw(Perl::Type::String); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | |||
| 23 | # [[[ SWITCH CONTEXT BACK TO PRIMARY PACKAGE ]]] | ||
| 24 | package Perl::Type::String; | ||
| 25 | 5 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 26 | 5 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 27 | |||
| 28 | # [[[ INCLUDES ]]] | ||
| 29 | 5 | ▶ | use POSIX qw(floor); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | |||
| 31 | # DEV NOTE: do not put inside INIT{} block, because it will be "too late to run INIT block" in some cases, such as inside Catalyst | ||
| 32 | # DEV NOTE, CORRELATION #rp040: fix recursive dependencies of String.pm & HelperFunctions_cpp.pm, as triggered by ingy's Inline::create_config_file() system() call | ||
| 33 | # DEV NOTE, DO NOT REMOVE! [error] Caught exception in ShinyCMS::Controller::Code->run_command "Undefined subroutine &main::PerlTypes_SvPOKp called at /home/wbraswell/github_repos/rperl-latest/lib/Perl/Type/String.pm line 82." | ||
| 34 | #if (not ((exists $ARGV[0]) and (defined $ARGV[0]) and ((substr $ARGV[0], -7, 7) eq '_Inline'))) { | ||
| 35 | #if (0) { | ||
| 36 | 0 | ▶ | use Perl::HelperFunctions_cpp; # main::PerlTypes_SvPOKp |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 37 | Perl::HelperFunctions_cpp::cpp_load(); | ||
| 38 | #} | ||
| 39 | |||
| 40 | # [[[ EXPORTS ]]] | ||
| 41 | use Exporter 'import'; | ||
| 42 | our @EXPORT = qw(string_CHECK string_CHECKTRACE string_to_boolean string_to_nonsigned_integer string_to_integer string_to_number string_to_character string_to_string); | ||
| 43 | our @EXPORT_OK = qw(string_typetest0 string_typetest1); | ||
| 44 | |||
| 45 | # [[[ TYPE CHECKING ]]] | ||
| 46 | sub string_CHECK { | ||
| 47 | { my void $RETURN_TYPE }; | ||
| 48 | ( my $possible_string ) = @ARG; | ||
| 49 | if ( not( defined $possible_string ) ) { | ||
| 50 | # croak( "\nERROR EPV00, TYPE-CHECKING MISMATCH, PERLOPS_PERLTYPES:\nstring value expected but undefined/null value found,\ncroaking" ); | ||
| 51 | die( "\nERROR EPV00, TYPE-CHECKING MISMATCH, PERLOPS_PERLTYPES:\nstring value expected but undefined/null value found,\ndying\n" ); | ||
| 52 | } | ||
| 53 | if ( not( main::PerlTypes_SvPOKp($possible_string) ) ) { | ||
| 54 | # croak( "\nERROR EPV01, TYPE-CHECKING MISMATCH, PERLOPS_PERLTYPES:\nstring value expected but non-string value found,\ncroaking" ); | ||
| 55 | die( "\nERROR EPV01, TYPE-CHECKING MISMATCH, PERLOPS_PERLTYPES:\nstring value expected but non-string value found,\ndying\n" ); | ||
| 56 | } | ||
| 57 | return; | ||
| 58 | } | ||
| 59 | |||
| 60 | # DEV NOTE: avoid error for those packages which do NOT 'use RPerl', but instead do 'use Perl::Types' and 'use Perl::Config' and 'use perltypesconv' etc. | ||
| 61 | # "Undefined subroutine &Perl::Type::String::string_CHECKTRACE called at lib/Perl/Type/String.pm line XYZ [ in string_to_integer() below ] | ||
| 62 | sub string_CHECKTRACE { | ||
| 63 | { my void $RETURN_TYPE }; | ||
| 64 | ( my $possible_string, my $variable_name, my $subroutine_name ) = @ARG; | ||
| 65 | if ( not( defined $possible_string ) ) { | ||
| 66 | # croak( "\nERROR EPV00, TYPE-CHECKING MISMATCH, PERLOPS_PERLTYPES:\nstring value expected but undefined/null value found,\nin variable $variable_name from subroutine $subroutine_name,\ncroaking" ); | ||
| 67 | die( "\nERROR EPV00, TYPE-CHECKING MISMATCH, PERLOPS_PERLTYPES:\nstring value expected but undefined/null value found,\nin variable $variable_name from subroutine $subroutine_name,\ndying\n" ); | ||
| 68 | } | ||
| 69 | if ( not( main::PerlTypes_SvPOKp($possible_string) ) ) { | ||
| 70 | # croak( "\nERROR EPV01, TYPE-CHECKING MISMATCH, PERLOPS_PERLTYPES:\nstring value expected but non-string value found,\nin variable $variable_name from subroutine $subroutine_name,\ncroaking" ); | ||
| 71 | die( "\nERROR EPV01, TYPE-CHECKING MISMATCH, PERLOPS_PERLTYPES:\nstring value expected but non-string value found,\nin variable $variable_name from subroutine $subroutine_name,\ndying\n" ); | ||
| 72 | } | ||
| 73 | return; | ||
| 74 | } | ||
| 75 | |||
| 76 | # [[[ BOOLEANIFY ]]] | ||
| 77 | sub string_to_boolean { | ||
| 78 | { my boolean $RETURN_TYPE }; | ||
| 79 | (my string $input_string) = @ARG; | ||
| 80 | # string_CHECK($input_string); | ||
| 81 | string_CHECKTRACE( $input_string, '$input_string', 'string_to_boolean()' ); | ||
| 82 | $input_string =~ s/_//gxms; # remove underscores to allow them in $input_string, fixes "Argument isn't numeric in multiplication (*)" | ||
| 83 | if (($input_string * 1) == 0) { return 0; } | ||
| 84 | else { return 1; } | ||
| 85 | return; | ||
| 86 | } | ||
| 87 | |||
| 88 | # [[[ UNSIGNED INTEGERIFY ]]] | ||
| 89 | sub string_to_nonsigned_integer { | ||
| 90 | { my integer $RETURN_TYPE }; | ||
| 91 | (my string $input_string) = @ARG; | ||
| 92 | # string_CHECK($input_string); | ||
| 93 | string_CHECKTRACE( $input_string, '$input_string', 'string_to_nonsigned_integer()' ); | ||
| 94 | $input_string =~ s/_//gxms; # remove underscores to allow them in $input_string, fixes "Argument isn't numeric in multiplication (*)" | ||
| 95 | return (floor(abs ($input_string * 1)) * 1); | ||
| 96 | } | ||
| 97 | |||
| 98 | # [[[ INTEGERIFY ]]] | ||
| 99 | sub string_to_integer { | ||
| 100 | { my integer $RETURN_TYPE }; | ||
| 101 | (my string $input_string) = @ARG; | ||
| 102 | # string_CHECK($input_string); | ||
| 103 | string_CHECKTRACE( $input_string, '$input_string', 'string_to_integer()' ); | ||
| 104 | # DEV NOTE: must use double-casting via '* 1' below to avoid following errors | ||
| 105 | # ERROR EIV01, TYPE-CHECKING MISMATCH, CPPOPS_PERLTYPES & CPPOPS_CPPTYPES: | ||
| 106 | # integer value expected but non-integer value found, | ||
| 107 | # in variable input_sv from subroutine XS_unpack_integer(), | ||
| 108 | $input_string =~ s/_//gxms; # remove underscores to allow them in $input_string, fixes "Argument isn't numeric in multiplication (*)" | ||
| 109 | # return floor($input_string * 1); | ||
| 110 | return (floor($input_string * 1) * 1); | ||
| 111 | } | ||
| 112 | |||
| 113 | # [[[ NUMBERIFY ]]] | ||
| 114 | sub string_to_number { | ||
| 115 | { my number $RETURN_TYPE }; | ||
| 116 | (my string $input_string) = @ARG; | ||
| 117 | # string_CHECK($input_string); | ||
| 118 | string_CHECKTRACE( $input_string, '$input_string', 'string_to_number()' ); | ||
| 119 | $input_string =~ s/_//gxms; # remove underscores to allow them in $input_string, fixes "Argument isn't numeric in multiplication (*)" | ||
| 120 | return ($input_string * 1.0); | ||
| 121 | } | ||
| 122 | |||
| 123 | # [[[ CHARACTERIFY ]]] | ||
| 124 | sub string_to_character { | ||
| 125 | { my character $RETURN_TYPE }; | ||
| 126 | (my string $input_string) = @ARG; | ||
| 127 | # string_CHECK($input_string); | ||
| 128 | string_CHECKTRACE( $input_string, '$input_string', 'string_to_character()' ); | ||
| 129 | if ($input_string eq q{}) { return q{}; } | ||
| 130 | else { return (substr $input_string, 0, 1); } | ||
| 131 | return; | ||
| 132 | } | ||
| 133 | |||
| 134 | # [[[ STRINGIFY ]]] | ||
| 135 | sub string_to_string { | ||
| 136 | { my string $RETURN_TYPE }; | ||
| 137 | ( my string $input_string ) = @ARG; | ||
| 138 | # string_CHECK($input_string); | ||
| 139 | string_CHECKTRACE( $input_string, '$input_string', 'string_to_string()' ); | ||
| 140 | |||
| 141 | # Perl::diag("in PERLOPS_PERLTYPES string_to_string(), received \$input_string =\n$input_string\n\n"); | ||
| 142 | $input_string =~ s/\\/\\\\/gxms; # escape all back-slash \ characters with another back-slash \ character | ||
| 143 | $input_string =~ s/\'/\\\'/gxms; # escape all single-quote ' characters with a back-slash \ character | ||
| 144 | $input_string = "'$input_string'"; | ||
| 145 | |||
| 146 | # Perl::diag("in PERLOPS_PERLTYPES string_to_string(), bottom of subroutine, returning possibly-modified \$input_string =\n$input_string\n\n"); | ||
| 147 | return $input_string; | ||
| 148 | } | ||
| 149 | |||
| 150 | # [[[ TYPE TESTING ]]] | ||
| 151 | sub string_typetest0 { | ||
| 152 | { my string $RETURN_TYPE }; | ||
| 153 | my string $retval = 'Spice PERLOPS_PERLTYPES'; | ||
| 154 | |||
| 155 | # Perl::diag("in PERLOPS_PERLTYPES string_typetest0(), have \$retval = '$retval'\n"); | ||
| 156 | return $retval; | ||
| 157 | } | ||
| 158 | sub string_typetest1 { | ||
| 159 | { my string $RETURN_TYPE }; | ||
| 160 | ( my string $lucky_string ) = @ARG; | ||
| 161 | # string_CHECK($lucky_string); | ||
| 162 | string_CHECKTRACE( $lucky_string, '$lucky_string', | ||
| 163 | 'string_typetest1()' ); | ||
| 164 | |||
| 165 | # Perl::diag("in PERLOPS_PERLTYPES string_typetest1(), received \$lucky_string = '$lucky_string'\n"); | ||
| 166 | return ( string_to_string($lucky_string) . ' PERLOPS_PERLTYPES' ); | ||
| 167 | } | ||
| 168 | |||
| 169 | # DEV NOTE, CORRELATION #rp018: Perl::Type::*.pm files do not 'use RPerl;' and thus do not trigger the pseudo-source-filter contained in | ||
| 170 | # RPerl::CompileUnit::Module::Class::create_symtab_entries_and_accessors_mutators(), | ||
| 171 | # so *__MODE_ID() subroutines are hard-coded here instead of auto-generated there | ||
| 172 | package main; | ||
| 173 | use strict; | ||
| 174 | use warnings; | ||
| 175 | sub Perl__Type__String__MODE_ID { return 0; } | ||
| 176 | |||
| 177 | 1; # end of class | ||