Scalar-Type-1.0.1/000755 000765 000024 00000000000 14611537735 014103 5ustar00davidstaff000000 000000 Scalar-Type-1.0.1/GPL2.txt000644 000765 000024 00000043103 11741643243 015342 0ustar00davidstaff000000 000000 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. Scalar-Type-1.0.1/ARTISTIC.txt000644 000765 000024 00000013737 11741643243 016072 0ustar00davidstaff000000 000000 The "Artistic License" Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder as specified below. "Copyright Holder" is whoever is named in the copyright or copyrights for the package. "You" is you, if you're thinking about copying or distributing this Package. "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as uunet.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) give non-standard executables non-standard names, and clearly document the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. You may embed this Package's interpreter within an executable of yours (by linking); this shall be construed as a mere form of aggregation, provided that the complete Standard Version of the interpreter is so embedded. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whoever generated them, and may be sold commercially, and may be aggregated with this Package. If such scripts or library files are aggregated with this Package via the so-called "undump" or "unexec" methods of producing a binary executable image, then distribution of such an image shall neither be construed as a distribution of this Package nor shall it fall under the restrictions of Paragraphs 3 and 4, provided that you do not represent such an executable image as a Standard Version of this Package. 7. C subroutines (or comparably compiled subroutines in other languages) supplied by you and linked into this Package in order to emulate subroutines and variables of the language defined by this Package shall not be considered part of this Package, but are the equivalent of input as in Paragraph 6, provided these subroutines do not change the language in any way that would cause it to fail the regression tests for the language. 8. Aggregation of this Package with a commercial distribution is always permitted provided that the use of this Package is embedded; that is, when no overt attempt is made to make this Package's interfaces visible to the end user of the commercial distribution. Such use shall not be construed as a distribution of this Package. 9. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 10. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End Scalar-Type-1.0.1/Type.xs000644 000765 000024 00000002624 14367301641 015375 0ustar00davidstaff000000 000000 #include "EXTERN.h" #include "perl.h" #include "XSUB.h" SV* _scalar_type(SV* argument) { SV* rval; static char num_as_str[100]; /* potential buffer overflow on 256-bit machines :-) */ if(SvIOK(argument)) { if(SvPOK(argument)) { /* int is also a string, better see if it's not int-ified 007 */ sprintf( num_as_str, (SvIsUV(argument) ? "%" UVuf : "%" IVdf), (SvIsUV(argument) ? SvUVX(argument) : SvIVX(argument)) ); rval = ( (strcmp(SvPVX(argument), num_as_str)) == 0 ? newSVpv("INTEGER", 7) : newSVpv("SCALAR", 6) ); } else { rval = newSVpv("INTEGER", 7); } } else if(SvNOK(argument)) { if(SvPOK(argument)) { /* float is also a string, better see if it's not float-ified 007.5 */ sprintf(num_as_str, "%" NVgf, SvNVX(argument)); rval = ( (strcmp(SvPVX(argument), num_as_str)) == 0 ? newSVpv("NUMBER", 6) : newSVpv("SCALAR", 6) ); } else { rval = newSVpv("NUMBER", 6); } } else { rval = newSVpv("SCALAR", 6); } return rval; } MODULE = Scalar::Type PACKAGE = Scalar::Type PROTOTYPES: DISABLE SV * _scalar_type (argument) SV * argument Scalar-Type-1.0.1/README000644 000765 000024 00000001264 14517531644 014764 0ustar00davidstaff000000 000000 This module grovels in perl's guts to see if a variable really is a number, as opposed to a string that just looks like a number, and whether it is an integer or not. ie, it can tell the difference between "100", 100, and 100.0. 100 is an integer. "100" is a string that looks like an integer. 100.0 is numerically equivalent to 100, but it is only accurate to 4 significant figures. To install, do the usual: perl Makefile.PL make make test make install You may use, modify and distribute this code under the terms of either the GNU General Public Licence, version 2, or the Artistic Licence. The full text of these can be found in the files GPL2.txt and ARTISTIC.txt respectively. Scalar-Type-1.0.1/MANIFEST.SKIP000644 000765 000024 00000000055 14367301641 015772 0ustar00davidstaff000000 000000 .travis.yml ^\.git .appveyor.yml .cirrus.yml Scalar-Type-1.0.1/MANIFEST000644 000765 000024 00000000652 14611537735 015237 0ustar00davidstaff000000 000000 lib/Scalar/Type.pm Type.xs t/types.t README MANIFEST Makefile.PL ARTISTIC.txt CHANGELOG GPL2.txt MANIFEST.SKIP t/pod-coverage.t t/pod.t t/sizeof.t t/bool.t lib/Test2/Tools/Type.pm t/test2-tools-type.t lib/Test2/Compare/Type.pm lib/Test2/Tools/Type/Extras.pm META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) Scalar-Type-1.0.1/t/000755 000765 000024 00000000000 14611537734 014345 5ustar00davidstaff000000 000000 Scalar-Type-1.0.1/META.yml000644 000765 000024 00000001401 14611537735 015350 0ustar00davidstaff000000 000000 --- abstract: unknown author: - unknown build_requires: ExtUtils::MakeMaker: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 7.70, CPAN::Meta::Converter version 2.150010' license: unknown meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Scalar-Type no_index: directory: - t - inc requires: Capture::Tiny: '0' Carp: '0' Devel::Peek: '0' Scalar::Util: '0' Test2::API: '1.302198' Test2::V0: '0.000159' if: '0' resources: bugtracker: https://github.com/DrHyde/perl-modules-Scalar-Type/issues repository: https://github.com/DrHyde/perl-modules-Scalar-Type version: v1.0.1 x_serialization_backend: 'CPAN::Meta::YAML version 0.018' Scalar-Type-1.0.1/META.json000644 000765 000024 00000002473 14611537735 015532 0ustar00davidstaff000000 000000 { "abstract" : "unknown", "author" : [ "unknown" ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 7.70, CPAN::Meta::Converter version 2.150010", "license" : [ "unknown", "open_source" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "Scalar-Type", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "Capture::Tiny" : "0", "Carp" : "0", "Devel::Peek" : "0", "Scalar::Util" : "0", "Test2::API" : "1.302198", "Test2::V0" : "0.000159", "if" : "0" } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/DrHyde/perl-modules-Scalar-Type/issues" }, "repository" : { "url" : "https://github.com/DrHyde/perl-modules-Scalar-Type" } }, "version" : "v1.0.1", "x_serialization_backend" : "JSON::PP version 4.16" } Scalar-Type-1.0.1/lib/000755 000765 000024 00000000000 14611537734 014650 5ustar00davidstaff000000 000000 Scalar-Type-1.0.1/Makefile.PL000644 000765 000024 00000001252 14611536011 016037 0ustar00davidstaff000000 000000 use 5.008; use ExtUtils::MakeMaker; WriteMakefile( NAME => 'Scalar::Type', META_MERGE => { license => 'open_source', resources => { repository => 'https://github.com/DrHyde/perl-modules-Scalar-Type', bugtracker => 'https://github.com/DrHyde/perl-modules-Scalar-Type/issues' }, }, VERSION_FROM => 'lib/Scalar/Type.pm', OBJECT => 'Type$(OBJ_EXT)', PREREQ_PM => { 'Scalar::Util' => 0, 'Devel::Peek' => 0, 'Capture::Tiny' => 0, 'Carp' => 0, 'if' => 0, 'Test2::API' => 1.302198, 'Test2::V0' => 0.000159, }, ); Scalar-Type-1.0.1/CHANGELOG000644 000765 000024 00000002542 14611536233 015310 0ustar00davidstaff000000 000000 1.0.1 2024-04-22 - Add Test2::Tools::Type 1.0.0 2023-10-29 - change: trying to use is_bool on a perl that doesn't support it is now a fatal error at import-time - bugfix: clarified some inconsistent text about licencing 0.3.2 2022-03-10 - bugfix: cope with some BOOL changes in blead-perl 0.3.1 2022-01-07 - add the bool_supported method that can be used to detect whether the BOOL type is supported 0.3.0 2022-01-07 - add support for BOOL type on perl 5.35.7 and later 0.2.0 2021-11-08 - add sizeof() function for numeric types 0.1.2 2021-06-22 - bugfix: pay attention to the IsUV flag for numified strings 0.1.1 2021-06-03 - bugfix: all functions require an argument; to not supply one is a fatal error. Previously they would erroneously behave as of you passed in `undef`. - bugfix: 0 is correctly treated as not being `undef`. 0.1.0 2021-05-25 - type() can return UNDEF - doco improvements 0.0.4 2021-05-07 - bugfix: internally use the correct sprintf format for stringifying floats - improvement: switch from Inline::C to XS so built module can be deployed to machines without a C compiler 0.0.3 2021-05-06 - cope with edge cases where perl fills an IN/NV slot but a variable should still be treated as a string 0.0.2 2021-05-01 Fix tests on 32 bit machines 0.0.1 2021-05-01 Initial release Scalar-Type-1.0.1/lib/Scalar/000755 000765 000024 00000000000 14611537734 016055 5ustar00davidstaff000000 000000 Scalar-Type-1.0.1/lib/Test2/000755 000765 000024 00000000000 14611537734 015651 5ustar00davidstaff000000 000000 Scalar-Type-1.0.1/lib/Test2/Tools/000755 000765 000024 00000000000 14611537734 016751 5ustar00davidstaff000000 000000 Scalar-Type-1.0.1/lib/Test2/Compare/000755 000765 000024 00000000000 14611537734 017237 5ustar00davidstaff000000 000000 Scalar-Type-1.0.1/lib/Test2/Compare/Type.pm000644 000765 000024 00000005567 14611536110 020516 0ustar00davidstaff000000 000000 package Test2::Compare::Type; use strict; use warnings; use base 'Test2::Compare::Base'; our $VERSION = '1.0.1'; use Test2::Compare qw(compare strict_convert); use Test2::Compare::Negatable; use Test2::Tools::Type (); use Test2::Util::HashBase qw(type); use Scalar::Type qw(bool_supported); use Scalar::Util qw(reftype blessed); use Carp qw(croak); sub init { my $self = shift; croak "'type' requires at least one argument" unless(@{$self->{+TYPE}}); foreach my $type (@{$self->{+TYPE}}) { croak "'$type' is not a valid argument, must either be Test2::Tools::Type checkers or Test2::Compare::* object" unless( Test2::Tools::Type->can("is_$type") || (blessed($type) && $type->isa('Test2::Compare::Base')) ); } $self->SUPER::init(); } sub name { join( " and ", map { blessed($_) ? blessed($_) : $_ } @{shift->{+TYPE}} ); } sub operator { join(' ', 'is', (shift->{+NEGATE} ? 'not' : ()), 'of type') } sub verify { my $self = shift; my %params = @_; my ($got, $exists) = @params{qw/got exists/}; return 0 unless $exists; my $result = 1; foreach my $type (@{$self->{+TYPE}}) { if(Test2::Tools::Type->can("is_$type")) { local $Test2::Compare::Type::verifying = 1; no strict 'refs'; $result &&= "Test2::Tools::Type::is_$type"->($got); } else { $result &&= !compare($got, $type, \&strict_convert); } } $result = !$result if($self->{+NEGATE}); return $result; } 1; =head1 NAME Test2::Compare::Type - Use a type to validate values in a deep comparison. =head1 DESCRIPTION This allows you to validate a value's type in a deep comparison. Sometimes a value just needs to look right, it may not need to be exact. An example is that you care that your code always returns an integer, but you don't care whether it is 192 or 3. =head1 CAVEATS The definitions of Boolean, integer and number are exactly the same as those in L, which this is a thin wrapper around. =head1 SEE ALSO L L L =head1 BUGS If you find any bugs please report them on Github, preferably with a test case. =head1 FEEDBACK I welcome feedback about my code, especially constructive criticism. =head1 AUTHOR, COPYRIGHT and LICENCE Mostly cargo-culted from L. Differences from that are Copyright 2024 David Cantrell EFE This software is free-as-in-speech software, and may be used, distributed, and modified under the terms of either the GNU General Public Licence version 2 or the Artistic Licence. It's up to you which one you use. The full text of the licences can be found in the files GPL2.txt and ARTISTIC.txt, respectively. =head1 CONSPIRACY This module is also free-as-in-mason software. =cut Scalar-Type-1.0.1/lib/Test2/Tools/Type.pm000644 000765 000024 00000016103 14611536074 020225 0ustar00davidstaff000000 000000 package Test2::Tools::Type; use strict; use warnings; use base qw(Exporter); our $VERSION = '1.0.1'; use Carp qw(croak); use Test2::API qw(context); use Test2::Compare::Type (); use Scalar::Type qw(bool_supported); our @EXPORT = qw( is_integer is_number is_bool bool_supported type ); sub import { if(@_) { if(grep { $_ eq ':extras' } @_) { require Test2::Tools::Type::Extras; Test2::Tools::Type::Extras->import(); push @EXPORT, @Test2::Tools::Type::Extras::EXPORT; @_ = grep { $_ ne ':extras' } @_; } if(grep { $_ eq 'show_types' } @_) { print "Supported types:\n"; print " ".substr($_, 3)."\n" foreach(sort grep { /^is_/ } @EXPORT); return; } } goto &Exporter::import; } sub is_integer { _checker(\&Scalar::Type::is_integer, @_); } sub is_number { _checker(\&Scalar::Type::is_number, @_); } sub is_bool { croak("You need perl 5.36 or higher to use is_bool") unless(bool_supported()); _checker(\&Scalar::Type::is_bool, @_); } sub _checker { my($checker, $candidate, $name) = @_; my $result = $checker->($candidate); # if we're coming from Test2::Compare::Type just do the check, don't # get/twiddle/release a context return $result if($Test2::Compare::Type::verifying); my $ctx = context(); return $ctx->pass_and_release($name) if($result); return $ctx->fail_and_release($name); } sub type { my @caller = caller; return Test2::Compare::Type->new( file => $caller[1], lines => [$caller[2]], type => \@_, ); } 1; =head1 NAME Test2::Tools::Type - Tools for checking data types =head1 SYNOPSIS use Test2::V0; use Test2::Tools::Type; is_integer(1, "is 1 integer?"); # pass, yes it is is_integer('1', "is '1' an integer?"); # fail, no it's a string SKIP: { skip "Your perl is too old" unless(bool_supported()); is_bool(1 == 2, "is false a Boolean?"); # pass, yes it is is_bool(3.1415, "is pi a Boolean?"); # fail, no it isn't } like { should_be_int => 1, other_stuff => "we don't care about this" }, hash { field should_be_int => type('integer'); }, "is the should_be_int field an integer?"; or if you want even more check functions: use Test2::V0; use Test2::Tools::Type qw(:extras); is_hashref($foo); =head1 OVERVIEW Sometimes you don't want to be too precise in your tests, you just want to check that your code returns the right type of result but you don't care whether it's returning 192 or 193 - just checking that it returns an integer is good enough. =head1 FUNCTIONS All these are exported by default. =head2 bool_supported Returns true if your perl is recent enough to have the Boolean type, false otherwise. It will be true if your perl is version 5.35.7 or higher. =head2 is_bool Emits a test pass if its argument is a Boolean - ie is the result of a comparison - and a fail otherwise. It is a fatal error to call this on a perl that is too old. If your tests need to run on perl 5.35.6 or earlier then you will need to check C before using it. See the L above. =head2 is_integer Emits a test pass if its argument is an integer and a fail otherwise. Note that it can tell the difference between C<1> (an integer) and C<'1'> (a string). =head2 is_number Emits a test pass if its argument is a number and a fail otherwise. Note that it can tell the difference between C<1> (a number), C<1.2> (also a number) and C<'1'> (a string). =head2 type Returns a check that you can use in a test such as: like { int => 1 }, hash { field int => type('integer'); }, "the 'int' field is an integer"; You can negate the test with a C thus. This test will fail: like { int => 1 }, hash { field int => !type('integer'); }, "the 'int' field is an integer"; You can supply more than one argument, so if you want to check that something is a I integer, for example, you can do: is(94, type(qw(positive integer))); You can check something's type and value: # this uses 'number' from Test2::Tools::Compare is($foo, type('integer', number(94))); And indeed you can use any other Test2 checker: # 'in_set' also comes from Test2::Tools::Compare is($foo, type('integer', in_set(1, 5, 8))); Valid arguments are any other Test2 checker (specifically, anything that inherits from L), and any of the C methods' names, with the leading C removed. You can see a list of supported types thus: $ perl -MTest2::Tools::Type=show_types or to include the extra functions: $ perl -MTest2::Tools::Type=show_types,:extras =head1 EXTRA FUNCTIONS By default the only check functions you get are those that are thin wrappers around L. If you pass the C<:extras> argument at C-time then all the following are available as well: =head2 regex_supported Returns true if your perl can reliably report the difference between a regex and a reference to a scalar, or false otherwise. It will be true if your perl is version 5.12 or higher. =head2 is_positive, is_negative Emit a test pass/fail depending on the argument's sign. Note that C<0> is considered neither positive nor negative. =head2 is_zero Emit a pass/fail depending on whether the argument is zero. =head2 is_ref Emit a pass/fail depending on whether the argument is a reference. This includes blessed objects. =head2 is_object Emit a pass/fail depending on whether the argument is a blessed object. =head2 is_regex Emit a test pass if its argument is a regex, and a fail otherwise. It is a fatal error to call this on a perl that is too old. If your tests need to run on perl 5.10.1 or earlier then you will need to check C before using it. =head2 is_hashref, is_arrayref, is_scalarref, is_coderef, is_globref, is_regex, is_refref Emit a pass/fail if the argumet is a reference to something of the appropriate type. =head1 CAVEATS The definitions of Boolean, integer and number are exactly the same as those in L, which this is a thin wrapper around. Blessed objects will match both C and the appropriate C. If you need to check that something is a ref, but is I blessed, do something like: is($foo, type(hashref => !type('object'))); =head1 SEE ALSO L L =head1 BUGS If you find any bugs please report them on Github, preferably with a test case. =head1 FEEDBACK I welcome feedback about my code, especially constructive criticism. =head1 AUTHOR, COPYRIGHT and LICENCE Copyright 2024 David Cantrell EFE This software is free-as-in-speech software, and may be used, distributed, and modified under the terms of either the GNU General Public Licence version 2 or the Artistic Licence. It's up to you which one you use. The full text of the licences can be found in the files GPL2.txt and ARTISTIC.txt, respectively. =head1 CONSPIRACY This module is also free-as-in-mason software. =cut Scalar-Type-1.0.1/lib/Test2/Tools/Type/000755 000765 000024 00000000000 14611537734 017672 5ustar00davidstaff000000 000000 Scalar-Type-1.0.1/lib/Test2/Tools/Type/Extras.pm000644 000765 000024 00000005461 14611536103 021471 0ustar00davidstaff000000 000000 package Test2::Tools::Type::Extras; use strict; use warnings; use base qw(Exporter); use Carp qw(croak); use Scalar::Util qw(blessed reftype); our $VERSION = '1.0.1'; our @EXPORT = ('regex_supported'); my @targets = map { $_.'::' } @_; { no strict 'refs'; while(my($k, $v) = each(%{__PACKAGE__.'::'})) { push @EXPORT, $k if( $k =~ /^is_/ && ref($v) ne 'SCALAR' && defined(&{$v}) ); } } *_checker = \&Test2::Tools::Type::_checker; sub is_positive { _checker(sub { $_[0] > 0 }, @_); } sub is_negative { _checker(sub { $_[0] < 0 }, @_); } sub is_zero { _checker(sub { $_[0] == 0; }, @_); } # There are tests that is_ref, is_object and is_hashref don't screw with # the argument's type. That covers the implementations that use # ref/blessed/reftype. If you add more checks with different innards, # add tests for that as well as that they return what you expect. sub is_ref { _checker(sub { ref($_[0]); }, @_); } sub is_object { _checker(sub { blessed($_[0]); }, @_); } sub is_hashref { _checker(sub { reftype($_[0]) && reftype($_[0]) eq 'HASH' }, @_); } sub is_arrayref { _checker(sub { reftype($_[0]) && reftype($_[0]) eq 'ARRAY' }, @_); } sub is_scalarref { _checker(sub { reftype($_[0]) && reftype($_[0]) eq 'SCALAR' }, @_); } sub is_coderef { _checker(sub { reftype($_[0]) && reftype($_[0]) eq 'CODE' }, @_); } sub is_globref { _checker(sub { reftype($_[0]) && reftype($_[0]) eq 'GLOB' }, @_); } sub is_refref { _checker(sub { reftype($_[0]) && reftype($_[0]) eq 'REF' }, @_); } sub is_regex { croak("You need perl 5.12 or higher to use is_regex") unless(regex_supported()); _checker(sub { reftype($_[0]) && reftype($_[0]) eq 'REGEXP' }, @_); } sub regex_supported { $] >= 5.012 } 1; =head1 NAME Test2::Tools::Type::Extras - Extra tools for checking data types =head1 SYNOPSIS use Test2::V0; use Test2::Tools::Type qw(:extras); =head1 DESCRIPTION This provides extra testing functions for Test2::Tools::Type. They can all be used stand-alone or with Test2::Tools::Type's C method. This module is not intended for you to use it directly. =head1 BUGS If you find any bugs please report them on Github, preferably with a test case. =head1 FEEDBACK I welcome feedback about my code, especially constructive criticism. =head1 AUTHOR, COPYRIGHT and LICENCE Copyright 2024 David Cantrell EFE This software is free-as-in-speech software, and may be used, distributed, and modified under the terms of either the GNU General Public Licence version 2 or the Artistic Licence. It's up to you which one you use. The full text of the licences can be found in the files GPL2.txt and ARTISTIC.txt, respectively. =head1 CONSPIRACY This module is also free-as-in-mason software. =cut Scalar-Type-1.0.1/lib/Scalar/Type.pm000644 000765 000024 00000031175 14611536011 017326 0ustar00davidstaff000000 000000 package Scalar::Type; use strict; use warnings; our $IS_BOOL_FN; # these shenanigans can be pared back a bit once 5.36 is out BEGIN { $IS_BOOL_FN = $] >= 5.035010 ? 'builtin::is_bool' : $] >= 5.035007 ? 'builtin::isbool' : 0 } sub bool_supported { $IS_BOOL_FN; } # 5.35.7 has this without the underscore, and it's not yet marked experimental use if bool_supported() eq 'builtin::isbool', qw(builtin); # 5.35.10-to-be has the underscore, and it's experimental use if bool_supported() eq 'builtin::is_bool', qw(experimental builtin); use Carp qw(croak); use Config; our $VERSION = '1.0.1'; require XSLoader; XSLoader::load(__PACKAGE__, $VERSION); use Scalar::Util qw(blessed); use base qw(Exporter); =head1 NAME Scalar::Type - figure out what type a scalar is =head1 SYNOPSIS use Scalar::Type qw(is_number); if(is_number(2)) { # yep, 2 is a number # it is_integer too } if(is_number("2")) { # no, "2" is a string } =head1 OVERVIEW Perl scalars can be either strings or numbers, and normally you don't really care which is which as it will do all the necessary type conversions automagically. This means that you can perform numeric operations on strings and provided that they B a number you'll get a sensible result: my $string = "4"; my $number = 1; my $result = $string + $number; # 5 But in some rare cases, generally when you are serialising data, the difference matters. This package provides some useful functions to help you figure out what's what. The following functions are available. None of them are exported by default. If you want all that are available, export ':all': use Scalar::Type qw(:all); and if you just want the 'is_*' functions you can get them all in one go: use Scalar::Type qw(is_*); For Reasons, C<:is_*> is equivalent. =cut our @EXPORT_OK = qw( type sizeof is_integer is_number bool_supported ); push @EXPORT_OK, 'is_bool' if(bool_supported()); our %EXPORT_TAGS = ( all => \@EXPORT_OK, 'is_*' => [grep { /^is_/ } @EXPORT_OK] ); sub import { __PACKAGE__->export_to_level(1, map { $_ eq 'is_*' ? ':is_*' : $_ } @_); } =head1 FUNCTIONS All of these functions require an argument. It is a fatal error to call them without. =head2 type Returns the type of its argument. If the argument is a reference then it returns either C (if it's an object), or C<'REF_TO_'.ref($argument)>. If the argument is C then it returns C<'UNDEF'>. If you are using perl 5.35.7 or later and the argument is the result of a comparison then it returns C<'BOOL'>. Otherwise it looks for the IOK or NOK flags on the underlying SV (see L for the exact mechanics) and returns C or C as appropriate. Finally, if neither of those are set it returns C. =head2 bool_supported Returns true if the C<'BOOL'> type is supported on this perl (ie if your perl version is 5.35.7 or later) and false otherwise. =cut sub type { croak(__PACKAGE__."::type requires an argument") if($#_ == -1); my $arg = shift; no strict 'refs'; return blessed($arg) ? blessed($arg) : ref($arg) ? 'REF_TO_'.ref($arg) : !defined($arg) ? 'UNDEF' : (bool_supported && &{$IS_BOOL_FN}($arg)) ? 'BOOL' : _scalar_type($arg); } =head2 sizeof Returns the size, in bytes, of the underlying storage for numeric types, and die()s for any other type. =cut sub sizeof { croak(__PACKAGE__."::sizeof requires an argument") if($#_ == -1); my $arg = shift; my $type = type($arg); if($type eq 'INTEGER') { return $Config{ivsize}; } elsif($type eq 'NUMBER') { return $Config{nvsize}; } else { croak(__PACKAGE__."::sizeof: '$arg' isn't numeric: ".type($arg)."\n"); } } =head2 is_integer Returns true if its argument is an integer. Note that "1" is not an integer, it is a string. 1 is an integer. 1.1 is obviously not an integer. 1.0 is also not an integer, as it makes a different statement about precision - 1 is *exactly* one, but 1.0 is only one to two significant figures. All integers are of course also numbers. =cut sub is_integer { croak(__PACKAGE__."::is_integer requires an argument") if($#_ == -1); type(@_) eq 'INTEGER' ? 1 : 0; } =head2 is_number Returns true if its argument is a number. "1" is not a number, it is a string. 1 is a number. 1.0 and 1.1 are numbers too. =cut sub is_number { croak(__PACKAGE__."::is_number requires an argument") if($#_ == -1); is_integer(@_) || type(@_) eq 'NUMBER' ? 1 : 0; } =head2 is_bool This is not available on perl versions earlier than 5.35.7. It is a fatal error to call this or try to import it on older perls. Returns true if its argument is a Boolean - ie, the result of a comparison. =cut sub is_bool { croak(__PACKAGE__."::is_bool not supported on your perl") if(!bool_supported); croak(__PACKAGE__."::is_bool requires an argument") if($#_ == -1); type(@_) eq 'BOOL'; } =head1 GORY DETAILS =head2 PERL VARIABLE INTERNALS As far as Perl code is concerned scalars will present themselves as integers, floats or strings on demand. Internally scalars are stored in a C structure, called an SV (scalar value), which contains several slots. The important ones for our purposes are: =over =item IV an integer value =item UV an unsigned integer value, only used for ints > MAXINT / 2. =item NV a numeric value (ie a float) =item PV a pointer value (ie a string) =back When a value is created one of those slots will be filled. As various operations are done on a value the slot's contents may change, and other slots may be filled. For example: my $foo = "4"; # fill $foo's PV slot, as "4" is a string my $bar = $foo + 1; # fill $bar's IV slot, as 4 + 1 is an int, # and fill $foo's IV slot, as we had to figure # out the numeric value of the string $foo = "lemon"; # fill $foo's PV slot, as "lemon" is a string That last operation immediately shows a problem. C<$foo>'s IV slot was filled with the integer value C<4>, but the assignment of the string C<"lemon"> only filled the PV slot. So what's in the IV slot? There's a handy tool for that, L, which is distributed with perl. Here's part of Devel::Peek's output: $ perl -MDevel::Peek -E 'my $foo = 4; $foo = "lemon"; Dump($foo);' IV = 4 PV = 0x7fe6e6c04c90 "lemon"\0 So how, then, does perl know that even thought there's a value in the IV slot it shouldn't be used? Because once you've assigned C<"lemon"> to the variable you can't get that C<4> to show itself ever again, at least not from pure perl code. The SV also has a flags field, which I missed out above. (I've also missed out some of the flags here, I'm only showing you the relevant ones): $ perl -MDevel::Peek -E 'my $foo = 4; $foo = "lemon"; Dump($foo);' FLAGS = (POK) IV = 4 PV = 0x7fe6e6c04c90 "lemon"\0 The C flag means, as you might have guessed, that the C slot has valid contents - in case you're wondering, the C slot there contains a pointer to the memory address C<0x7fe6e6c04c90>, at which can be found the word C. It's possible to have multiple flags set. That's the case in the second line of code in the example. In that example a variable contains the string C<"4">, so the C slot is filled and the C flag is set. We then take the value of that variable, add 1, and assign the result to another variable. Obviously adding 1 to a string is meaningless, so the string has to first be converted to a number. That fills the C slot: $ perl -MDevel::Peek -E 'my $foo = "4"; my $bar = $foo + 1; Dump($foo);' FLAGS = (IOK,POK) IV = 4 PV = 0x7fd6e7d05210 "4"\0 Notice that there are now two flags. C means that the C slot's contents are valid, and C that the C slot's contents are valid. Why do we need both slots in this case? Because a non-numeric string such as C<"lemon"> is treated as the integer C<0> if you perform numeric operations on it. All that I have said above about Cs also applies to Cs, and you will sometimes come across a variable with both the C and C slots filled, or even all three: $ perl -MDevel::Peek -E 'my $foo = 1e2; my $bar = $foo + 0; $bar = $foo . ""; Dump($foo)' FLAGS = (IOK,NOK,POK) IV = 100 NV = 100 PV = 0x7f9ee9d12790 "100"\0 Finally, it's possible to have multiple flags set even though the slots contain what looks (to a human) like different values: $ perl -MDevel::Peek -E 'my $foo = "007"; $foo + 0; Dump($foo)' FLAGS = (IOK,POK) IV = 7 PV = 0x7fcf425046c0 "007"\0 That code initialises the variable to the string C<"007">, then uses it in a numeric operation. That causes the string to be numified, the C slot to be filled, and the C flag set. It should, of course, be clear to any fan of classic literature that "007" and 7 are very different things. "007" is not an integer. =head3 Booleans In perl 5.35.7 and later, Boolean values - ie the results of comparisons - have some extra magic. As well as their value, which is either C<1> (true, an integer) or C<''> (false, an empty string), they have a flag to indicate their Booleanness. This is exposed via the C perl function so we don't need to do XS voodoo to interrogate it. =head2 WHAT Scalar::Type DOES (at least in version 1.0.0) NB that this section documents an internal function that is not intended for public use. The interface of C<_scalar_type> should be considered to be unstable, not fit for human consumption, and subject to change without notice. This documentation is correct as of version 1.0.0 but may not be updated for future versions - its purpose is pedagogical only. The C functions are just wrappers around the C function. That in turn delegates most of the work to a few lines of C code which grovel around looking at the contents of the individual slots and flags. That function isn't exported, but if you really want to call it directly it's called C<_scalar_type> and will return one of three strings, C, C, or C. It will return C even for a reference or undef, which is why I said that the C function only *mostly* wraps around it :-) The first thing that C<_scalar_type> does is look at the C flag. If it's set, and the C flag is not set, then it returns C. If C and C are set it stringifies the contents of the C slot, compares to the contents of the C slot, and returns C if they are the same, or C otherwise. The reason for jumping through those hoops is so that we can correctly divine the type of C<"007"> in the last example above. If C isn't set we then look at C. That follows exactly the same logic, looking also at C, and returning either C or C, being careful about strings like C<"007.5">. If neither C nor C is set then we return C. And what about Cs? They are treated exactly the same as Cs, and a variable with a valid C slot will have the B> flag set. It will also have the C flag set, which we use to determine how to stringify the number. =head1 SEE ALSO L, which is bundled with this module. L in particular its C function. L if you have perl 5.35.7 or later. =head1 BUGS If you find any bugs please report them on Github, preferably with a test case. Integers that are specifed using exponential notation, such as if you say 1e2 instead of 100, are *not* internally treated as integers. The perl parser is lazy and only bothers to convert them into an integer after you perform int-ish operations on them, such as adding 0. Likewise if you add 0 to the thoroughly non-numeric "100" perl will convert it to an integer. These edge cases are partly why you almost certainly don't care about what this module does. If they irk you, complain to p5p. =head1 FEEDBACK I welcome feedback about my code, especially constructive criticism. =head1 AUTHOR, COPYRIGHT and LICENCE Copyright 2024 David Cantrell EFE This software is free-as-in-speech software, and may be used, distributed, and modified under the terms of either the GNU General Public Licence version 2 or the Artistic Licence. It's up to you which one you use. The full text of the licences can be found in the files GPL2.txt and ARTISTIC.txt, respectively. =head1 CONSPIRACY This module is also free-as-in-mason software. =cut 1; Scalar-Type-1.0.1/t/pod.t000644 000765 000024 00000000313 14611536011 015274 0ustar00davidstaff000000 000000 use Test2::V0; eval "use Test::Pod 1.18"; plan skip_all => "Test::Pod 1.18 required for testing POD" if $@; all_pod_files_ok(grep { $_ !~ m{Number/Phone/UK/Data.pm$} } all_pod_files()); done_testing(); Scalar-Type-1.0.1/t/types.t000644 000765 000024 00000017640 14611536011 015671 0ustar00davidstaff000000 000000 use Test2::V0; no warnings 'void'; use Scalar::Type qw(is_* type); use B; use Capture::Tiny qw(capture_stderr); use Devel::Peek; select(STDERR); $| = 1; select(STDOUT); $| = 1; subtest "is_integer" => sub { ok(is_integer(0), '0 is an integer'); ok(is_integer(-0), '-0 is an integer'); ok(is_integer(1), '1 is an integer'); ok(is_integer(-1), '-1 is an integer'); ok(!is_integer("1"), '"1" is not an integer'); ok(!is_integer(1.0), '1.0 is not an integer'); ok(!is_integer(1.1), '1.1 is not an integer'); ok(!is_integer(0.0), '0.0 is not an integer'); ok(is_integer(0x12), '0x12 is an integer'); ok(!is_integer("0x12"), '"0x12" is not an integer'); # the IV slot in the SV got filled my $foo = "1"; $foo += 0; ok(is_integer($foo), '"1" + 0 gets its IV slot filled, is an integer'); undef $foo; ok(!is_integer($foo), "after undef-ing, it's no longer an integer"); }; subtest "is_number" => sub { ok(is_number(1), '1 is a number'); ok(is_number(1.0), '1.0 is a number'); ok(is_number(1.1), '1.1 is a number'); ok(is_number(0.0), '0.0 is a number'); ok(!is_number("1"), '"1" is not a number'); ok(!is_number("1.0"), '"1.0" is not a number'); ok(!is_number("0x12"), '"0x12" is not a number'); my $foo = 0x12; ok(is_number($foo), '0x12 is a number'); my $bar = "12.10"; ok(!is_number($bar), '"12.10" is not a number'); $bar + 0; ok(!is_number($bar), '"12.10" is still not a number after being used in a numeric context'); note(capture_stderr { Dump($bar) }); }; subtest "integers written as exponents are weird" => sub { # toke.c just assumes that if there's an e it must be a float. At some point it # would be nice to correct that, but not at the cost of "correcting" 1.0 into an int. # these tests are mostly just placeholders for when we do that. subtest "32 bit-friendly exp ints" => sub { ok(!is_integer(1e2), '1e2 is not an integer (but it oughta be!)'); ok(is_number(1e2), '... but it is a number'); ok(!is_integer(-1e2), '-1e2 is not an integer (but it oughta be!)'); ok(is_number(-1e2), '... but it is a number'); my $foo = 1e2; $foo += 0; ok(is_integer($foo), '1e2 + 0 is an integer'); $foo = 0; $foo += 1e2; ok(is_integer($foo), '0 + 1e2 is an integer'); $foo = -1e2; $foo += 0; ok(is_integer($foo), '-1e2 + 0 is an integer'); }; subtest "32 bit-unfriendly, 64 bit-friendly exp ints" => sub { ok(!is_integer(1e10), '1e10 is not an integer (but it oughta be on 64 bit machines!)'); ok(is_number(1e10), '... but it is a number'); ok(!is_integer(-1e10), '-1e10 is not an integer (but it oughta be on 64 bit machines!)'); ok(is_number(-1e10), '... but it is a number'); my $foo = 1e10; $foo += 0; if(~0 < $foo) { # 32 bit system ok(!is_integer($foo), '1e10 + 0 is not an integer because your computer is pathetic'); $foo = 0; $foo += 1e10; ok(!is_integer($foo), '0 + 1e10 is not an integer because your computer is pathetic'); $foo = -1e10; $foo += 0; ok(!is_integer($foo), '-1e10 + 0 is not an integer because your computer is pathetic'); } else { ok(is_integer($foo), '1e10 + 0 is an integer'); $foo = 0; $foo += 1e10; ok(is_integer($foo), '0 + 1e10 is an integer'); $foo = -1e10; $foo += 0; ok(is_integer($foo), '-1e10 + 0 is an integer'); } }; subtest "64 bit-unfriendly exp ints" => sub { ok(!is_integer(1e20), '1e20 is not an integer'); ok(is_number(1e20), '... but it is a number'); ok(!is_integer(-1e20), '-1e20 is not an integer'); ok(is_number(-1e20), '... but it is a number'); my $foo = 1e20; $foo += 0; ok(!is_integer($foo), '1e20 + 0 is not an integer'); ok(is_number($foo), '... but it is a number'); $foo = 0; $foo += 1e20; ok(!is_integer($foo), '0 + 1e20 is not an integer'); ok(is_number($foo), '... but it is a number'); $foo = -1e20; $foo += 0; ok(!is_integer($foo), '-1e20 + 0 is not an integer'); ok(is_number($foo), '... but it is a number'); }; }; subtest "string subsequently used as an int or float" => sub { subtest "'007'" => sub { my $foo = '007'; $foo < 8; ok($foo eq '007', "after being treated as an int it still has its original value"); is(type($foo), 'SCALAR', "and it's not become an integer as far as we're concerned"); note(capture_stderr { Dump($foo) }); }; subtest "'007.5'" => sub { my $foo = "007.5"; $foo + 0.5; ok($foo eq '007.5', "after being treated as a float it still has its original value"); is(type($foo), 'SCALAR', "and it's not become a float as far as we're concerned"); note(capture_stderr { Dump($foo) }); }; subtest "'7'" => sub { my $foo = '7'; $foo < 8; is(type($foo), 'INTEGER', "this does become an int after a numeric operation"); note(capture_stderr { Dump($foo) }); }; subtest "'7.5'" => sub { my $foo = '7.5'; $foo < 8; is(type($foo), 'NUMBER', "this does become a float after a numeric operation"); note(capture_stderr { Dump($foo) }); }; subtest "'[MAXINT]'" => sub { my $foo = ''.~0; $foo + 0; is(type($foo), 'INTEGER', "this becomes an integer after a numeric operation, even though the value is a UV (unsigned int), not an IV"); note(capture_stderr { Dump($foo) }); }; }; subtest "int subsequently used as a float" => sub { my $foo = 7; $foo + 0.5; ok($foo == 7, "after being treated as a float the variable still has its original value 7"); ok(is_integer($foo), "7 is still an integer after being numerically compared to a float"); }; subtest "are we checking the flags, not just the contents of the IV/NV slots?" => sub { my $foo = 42; ok(is_integer($foo), 'variable containing 42 is an integer'); ok(is_number($foo), "... and so of course it's also a number"); $foo = 'forty two'; ok(!is_integer($foo), 'variable is no longer an int after a string was assigned to it'); ok(!is_number($foo), '... no longer a number either'); note("still says 42 in the IV slot, but IOK isn't set"); note(capture_stderr { Dump($foo) }); $foo = 3.14; ok(!is_integer($foo), 'variable containing 3.14 is not an integer'); ok(is_number($foo), '... but it is a number'); $foo = 'delicious pie'; ok(!is_integer($foo), 'still not an integer after value changed to "delicious pie"'); ok(!is_number($foo), '... no longer a number either'); }; subtest "references" => sub { ok(!is_integer(\1), '\\1 is not an integer'); is(type(\1), 'REF_TO_SCALAR', '\\1 is of type REF_TO_SCALAR'); is(type(\"1"), 'REF_TO_SCALAR', '\\"1" is of type REF_TO_SCALAR'); is(type({}), 'REF_TO_HASH', '{} is of type REF_TO_HASH'); is(type(B::svref_2object(\1)), 'B::IV', 'blessed scalars return their class'); }; subtest "type returns the documented values for non-reference types" => sub { is(type(1), 'INTEGER', '1 is of type INTEGER'); is(type(1.0), 'NUMBER', '1.0 is of type NUMBER'); is(type(1.1), 'NUMBER', '1.1 is of type NUMBER'); is(type("1"), 'SCALAR', '"1" is of type SCALAR'); is(type("1.0"), 'SCALAR', '"1.0" is of type SCALAR'); is(type("1.1"), 'SCALAR', '"1.1" is of type SCALAR'); is(type(undef), 'UNDEF', 'undef is of type UNDEF'); }; like dies { type() }, qr{::type requires an argument at t/types.t line}, "type() requires an argument"; like dies { is_number() }, qr{::is_number requires an argument at t/types.t line}, "is_number() requires an argument"; like dies { is_integer() }, qr{::is_integer requires an argument at t/types.t line}, "is_integer() requires an argument"; done_testing; Scalar-Type-1.0.1/t/bool.t000644 000765 000024 00000002343 14611536011 015452 0ustar00davidstaff000000 000000 use Test2::V0; use Config; use Scalar::Type qw(:all); if(Scalar::Type::bool_supported) { is( type(1 == 1), 'BOOL', 'type(1 == 1) is BOOL' ); is( type(1 == 0), 'BOOL', 'type(1 == 0) is BOOL' ); ok(is_bool(1 == 1), 'is_bool says yes for (1 == 1)'); ok(is_bool(1 == 0), 'is_bool says yes for (1 == 0)'); ok(!is_bool(1), 'but it says no for plain old 1 (otherwise indistinguishable from (1 == 1))'); ok(!is_bool(''), "and it says no for plain old '' (otherwise indistinguishable from (1 == 0))"); } else { # the :all above only included is_bool if bool_supported so we need to use the full name here like dies { Scalar::Type::is_bool(1 == 1) }, qr/::is_bool not supported on your perl/, "is_bool carks it on Ye Olde Perle $]"; is( type(1 == 1), 'INTEGER', 'type(1 == 1) is INTEGER' ); is( type(1 == 0), 'SCALAR', 'type(1 == 0) is SCALAR' ); # finally, test that we can't explicitly import is_bool on Ye Olde Perle like dies { Scalar::Type->import('is_bool') }, qr/is_bool/, "can't import is_bool on Ye Olde Perle $]"; } done_testing; Scalar-Type-1.0.1/t/sizeof.t000644 000765 000024 00000001137 14611536011 016016 0ustar00davidstaff000000 000000 use Test2::V0; use Config; use Scalar::Type qw(sizeof); subtest "sizeof(integer)" => sub { is(sizeof(1), $Config{ivsize}, "sizeof(int) is correct"); }; subtest "sizeof(number)" => sub { is(sizeof(1.2), $Config{nvsize}, "sizeof(float) is correct"); }; subtest "sizeof(whatever)" => sub { like dies { sizeof() }, qr{::sizeof requires an argument at t/sizeof.t line}, "sizeof() requires an argument"; like dies { sizeof("banana") }, qr{::sizeof: 'banana' isn't numeric: SCALAR}, "sizeof()'s arg must be plausibly numeric"; }; done_testing; Scalar-Type-1.0.1/t/test2-tools-type.t000644 000765 000024 00000033344 14611536011 017702 0ustar00davidstaff000000 000000 use Test2::V0; use Test2::Tools::Type; # NB no :extras! use Test2::API qw/intercept/; use Capture::Tiny qw(capture); use Config; ok( dies { is_positive(1) }, "extra functions aren't available unless asked for" ); subtest "show supported types" => sub { my $types_supported = capture { Test2::Tools::Type->import(qw(show_types)) }; like $types_supported, match(qr/\n number\n/), "default types"; like $types_supported, !match(qr/\n positive\n/), "default types doesn't include the extras"; # this does *not* make extras available for the test of the tests # because `show_types` aborts import() before it can do anything $types_supported = capture { Test2::Tools::Type->import(qw(show_types :extras)) }; like $types_supported, match(qr/\n positive\n/), ":extras makes extras visible"; }; Test2::Tools::Type->import(qw(:extras)); subtest "is_* tests" => sub { my $events = intercept { is_integer(1, "wow, a pass!"); is_integer(1.2); is_integer("1", "fail"); is_number(1, "pass"); is_number(1.2, "pass"); is_number("1", "fail"); is_positive(1.2, "pass"); is_positive(-1.2, "fail"); is_negative(-1.2, "pass"); is_negative(1.2, "fail"); is_positive("1.2", "pass"); is_negative("-1.2", "pass"); is_ref(1); # fail is_ref(\1); # pass is_object(1); # fail is_object(bless {}); # pass is_hashref([]); # fail is_hashref({}); # pass is_arrayref({}); # fail is_arrayref([]); # pass is_scalarref(sub {}); # fail is_scalarref(\""); # pass is_coderef(\""); # fail is_coderef(sub {}); # pass is_globref(*is_integer); # fail is_globref(\*is_integer); # pass is_refref(\1); # fail is_refref(\\1); # pass if(regex_supported()) { is_regex(1); # fail is_regex(qr/abc/); # pass } if(bool_supported()) { is_bool(1==1, "pass"); is_bool(1==2, "pass"); is_bool(1, "fail"); is_integer(1==1, "fail"); is_number(1==1, "fail"); is_integer(1==2, "fail"); is_number(1==2, "fail"); } }; is($events->[0]->name, "wow, a pass!", "test names emitted correctly when supplied"); is($events->[1]->name, undef, "no name supplied? no name emitted"); foreach my $test ( { result => 'Pass', name => 'is_integer(1)' }, { result => 'Fail', name => 'is_integer(1.2)' }, { result => 'Fail', name => 'is_integer("1")' }, { result => 'Pass', name => 'is_number(1)' }, { result => 'Pass', name => 'is_number(1.2)' }, { result => 'Fail', name => 'is_number("1")' }, { result => 'Pass', name => 'is_positive(1.2)' }, { result => 'Fail', name => 'is_positive(-1.2)' }, { result => 'Pass', name => 'is_negative(-1.2)' }, { result => 'Fail', name => 'is_negative(1.2)' }, { result => 'Pass', name => 'is_positive("1.2")' }, { result => 'Pass', name => 'is_negative("-1.2")' }, { result => 'Fail', name => 'is_ref(1)' }, { result => 'Pass', name => 'is_ref(\\1)' }, { result => 'Fail', name => 'is_object(1)' }, { result => 'Pass', name => 'is_object(bless {})' }, { result => 'Fail', name => 'is_hashref([])' }, { result => 'Pass', name => 'is_hashref({})' }, { result => 'Fail', name => 'is_arrayref({})' }, { result => 'Pass', name => 'is_arrayref([])' }, { result => 'Fail', name => 'is_scalarref(sub {})' }, { result => 'Pass', name => 'is_scalarref(\"")' }, { result => 'Fail', name => 'is_coderef(\""' }, { result => 'Pass', name => 'is_coderef(sub {})' }, { result => 'Fail', name => 'is_globref(*is_integer)' }, { result => 'Pass', name => 'is_globref(\*is_integer)' }, { result => 'Fail', name => 'is_refref(\\1)' }, { result => 'Pass', name => 'is_refref(\\\\1)' }, { result => 'Fail', name => 'is_regex(1)', regex_required => 1 }, { result => 'Pass', name => 'is_regex(qr/abc/)', regex_required => 1 }, { result => 'Pass', name => 'is_bool(1==1)', bool_required => 1 }, { result => 'Pass', name => 'is_bool(1==2)', bool_required => 1 }, { result => 'Fail', name => 'is_bool(1)', bool_required => 1 }, { result => 'Fail', name => 'is_integer(1==1)', bool_required => 1 }, { result => 'Fail', name => 'is_number(1==1)', bool_required => 1 }, { result => 'Fail', name => 'is_integer(1==2)', bool_required => 1 }, { result => 'Fail', name => 'is_number(1==2)', bool_required => 1 }, ) { my $event = shift(@{$events}); SKIP: { skip "Your perl doesn't support the regex type" if($test->{regex_required} && !regex_supported()); skip "Your perl doesn't support the Boolean type" if($test->{bool_required} && !bool_supported()); isa_ok( $event, ["Test2::Event::".$test->{result}], $test->{name}."\t".$test->{result} ); } } if(!regex_supported()) { like dies { is_regex(1==1) }, qr/You need perl 5.12/, "is_regex: perl too old, exception"; } else { is_regex(qr/abc/, "regex_supported on your perl"); } if(!bool_supported()) { like dies { is_bool(1==1) }, qr/You need perl 5.36/, "is_bool: perl too old, exception"; } else { is_bool(1==1, "bool_supported on your perl"); } }; subtest "type() tests" => sub { my $events = intercept { # NB order is important! if you insert or remove tests, some of the checks # that failure messages are correct in like()s below may fail! is(1, type('integer')); # pass is(1.2, type('integer')); # fail is(1, !type('integer')); # fail is(1.2, !type('integer')); # pass is(1.2, type('number')); # pass is('1.2', !type('positive', 'number')); # pass is('1.2', type('positive', 'number')); # fail is(1.2, type('positive', 'number')); # pass is(-1.2, type('positive', 'number')); # fail is(-1.2, type('negative', 'number')); # pass is(-1.2, type('negative', 'integer')); # fail is(-1.2, type('number', number(-1.1))); # fail is(-1.2, type('number', number(-1.2))); # pass is(4, type(integer => in_set(1, 5, 8))); # fail is(4, type(integer => in_set(1, 4, 8))); # pass is( # pass { int => 1, chicken => 'bird', elephant => 'seal' }, type( 'hashref', hash { field int => 1; field chicken => 'bird'; field elephant => 'seal' } ) ); is( # fail { int => 1, chicken => 'bird', elephant => 'seal' }, type( 'hashref', hash { field int => 1; field chicken => 'coward'; } ) ); is( # fail bless({ int => 1, chicken => 'bird', elephant => 'seal' }), type( !type('object'), 'hashref', hash { field int => 1; field chicken => 'bird'; field elephant => 'seal'; } ) ); if(bool_supported()) { is(1==1, type('bool')); # pass is(1==2, type('bool')); # pass is(1.2, type('bool')); # fail } }; like( $events->[1]->info->[0]->details, qr/\bis of type .* integer\b/, "failed test, op and name emitted in diagnostics are correct" ); like( $events->[2]->info->[0]->details, qr/is not of type/, "failed negated test, op emitted in diagnostics is correct" ); like( $events->[6]->info->[0]->details, qr/\bis of type .* positive and number\b/, "failed test, op and multi-name emitted in diagnostics are correct" ); like( $events->[11]->info->[0]->details, qr/\bis of type .* number and Test2::Compare::Number /, "failed test, op and name with 'has value' emitted in diagnostics are correct" ); like( $events->[13]->info->[0]->details, qr/\bis of type .* integer and Test2::Compare::Set /, "failed test, op and name with another checker emitted in diagnostics are correct" ); foreach my $test ( { result => "Ok", name => "is(1, type('integer'))" }, { result => "Fail", name => "is(1.2, type('integer'))" }, { result => "Fail", name => "is(1, !type('integer'))" }, { result => "Ok", name => "is(1.2, !type('integer'))" }, { result => "Ok", name => "is(1.2, type('number'))" }, { result => "Ok", name => "is('1.2', !type('positive', 'number'))" }, { result => "Fail", name => "is('1.2', type('positive', 'number'))" }, { result => "Ok", name => "is(1.2, type('positive', 'number'))" }, { result => "Fail", name => "is(-1.2, type('positive', 'number'))" }, { result => "Ok", name => "is(-1.2, type('negative', 'number'))" }, { result => "Fail", name => "is(-1.2, type('negative', 'integer'))" }, { result => "Fail", name => "is(-1.2, type('number', -1.1))" }, { result => "Ok", name => "is(-1.2, type('number', -1.2))" }, { result => "Fail", name => "is(4, type(integer => in_set(1, 5, 8)))" }, { result => "Ok", name => "is(4, type(integer => in_set(1, 4, 8)))" }, { result => "Ok", name => "is({ ... }, type(hashref => { same }))" }, { result => "Fail", name => "is({ ... }, type(hashref => { not the same }))" }, { result => "Fail", name => "is(bless({ ... }), type(!type('object'), hashref => { same }))" }, { result => "Ok", name => "is(1==1, type('bool'))", bool_required => 1 }, { result => "Ok", name => "is(1==2, type('bool'))", bool_required => 1 }, { result => "Fail", name => "is(1.2, type('bool'))", bool_required => 1 }, ) { my $event = shift(@{$events}); SKIP: { skip "Your perl doesn't support the Boolean type" if($test->{bool_required} && !bool_supported()); isa_ok( $event, ["Test2::Event::".$test->{result}], $test->{name}."\t".$test->{result} ); } } if(!bool_supported()) { like dies { is(1, type('bool')) }, qr/You need perl 5.36/, "type('bool'): perl too old, exception"; } like dies { type() }, qr/'type' requires at least one argument/, "argument is mandatory"; like dies { type('mammal') }, qr/'mammal' is not a valid argument, must either be Test2::Tools::Type checkers or Test2::Compare::\* object/, "exception: 'mammal' isn't a valid argument to type()"; like dies { type('hashref' => { foo => 'bar' }) }, qr/'HASH.*' is not a valid argument/, "exception: random data isn't a valid argument either"; }; subtest "checks don't mess with types" => sub { my $events = intercept { my $integer = 1; is_integer($integer); # pass is_positive($integer); is_ref($integer); is_object($integer); is_hashref($integer); is_negative($integer); is_zero($integer); is($integer, !type(qw(integer positive negative zero))); # LOL is_integer($integer); # pass }; isa_ok( $events->[0], ['Test2::Event::Pass'], "starting with an int" ); isa_ok( $events->[-1], ['Test2::Event::Pass'], "is_{positive,negative,zero} and ref/reftype/blessed don't accidentally un-intify an int" ); $events = intercept { my $number = 1.1; is_integer($number); # fail is_positive($number); is_ref($number); is_object($number); is_hashref($number); is_negative($number); is_zero($number); is($number, type(qw(integer positive negative zero))); # LOL is_number($number); # pass }; isa_ok( $events->[0], ['Test2::Event::Fail'], "starting with a float" ); isa_ok( $events->[-1], ['Test2::Event::Pass'], "is_{positive,negative,zero} and ref/reftype/blessed don't accidentally intify a float" ); $events = intercept { my $string = "1.1"; is_number($string); # fail is_positive($string); is_ref($string); is_object($string); is_hashref($string); is_negative($string); is_zero($string); is($string, type(qw(integer positive negative zero))); # LOL is_number($string); # fail }; isa_ok( $events->[0], ['Test2::Event::Fail'], "starting with a string" ); isa_ok( $events->[-1], ['Test2::Event::Fail'], "is_{positive,negative,zero} and ref/reftype/blessed don't accidentally numify a string" ); }; done_testing; Scalar-Type-1.0.1/t/pod-coverage.t000644 000765 000024 00000000463 14611536011 017073 0ustar00davidstaff000000 000000 use Test2::V0; eval "use Test::Pod::Coverage 1.08"; plan skip_all => "Test::Pod::Coverage 1.08 required for testing POD coverage" if $@; foreach my $module (grep { $_ ne 'Test2::Compare::Type' && $_ ne 'Test2::Tools::Type::Extras' } all_modules()) { pod_coverage_ok($module); } done_testing();