GithubHelp home page GithubHelp logo

boolean-pm's Introduction

Name

boolean - Boolean support for Perl

boolean-pm

Version

This document describes boolean version 0.46.

Synopsis

use boolean;

do &always if true;
do &never if false;

do &maybe if boolean($value)->isTrue;

and:

use boolean ':all';

$guess = int(rand(2)) % 2 ? true : false;

do &something if isTrue($guess);
do &something_else if isFalse($guess);

Description

Most programming languages have a native Boolean data type. Perl does not.

Perl has a simple and well known Truth System. The following scalar values are false:

$false1 = undef;
$false2 = 0;
$false3 = 0.0;
$false4 = '';
$false5 = '0';

Every other scalar value is true.

This module provides basic Boolean support, by defining two special objects: true and false.

Rationale

When sharing data between programming languages, it is important to support the same group of basic types. In Perlish programming languages, these types include: Hash, Array, String, Number, Null and Boolean. Perl lacks native Boolean support.

Data interchange modules like YAML and JSON can now use boolean to encodedecoderoundtrip Boolean values.

Functions

This module defines the following functions:

true

This function returns a scalar value which will evaluate to true. The value is a singleton object, meaning there is only one "true" value in a Perl process at any time. You can check to see whether the value is the "true" object with the isTrue function described below.

false

This function returns a scalar value which will evaluate to false. The value is a singleton object, meaning there is only one "false" value in a Perl process at any time. You can check to see whether the value is the "false" object with the isFalse function described below.

boolean($scalar)

Casts the scalar value to a boolean value. If $scalar is true, it returns boolean::true, otherwise it returns boolean::false.

isTrue($scalar)

Returns boolean::true if the scalar passed to it is the boolean::true object. Returns boolean::false otherwise.

isFalse($scalar)

Returns boolean::true if the scalar passed to it is the boolean::false object. Returns boolean::false otherwise.

isBoolean($scalar)

Returns boolean::true if the scalar passed to it is the boolean::true or boolean::false object. Returns boolean::false otherwise.

Methods

Since true and false return objects, you can call methods on them.

$boolean->isTrue

Same as isTrue($boolean).

$boolean->isFalse

Same as isFalse($boolean).

Use Options

By default this module exports the true, false and boolean functions.

The module also defines these export tags:

:all

Exports true, false, boolean, isTrue, isFalse, isBoolean

Deprecations

This module offered an export tag, -truth, that overrides the Perl interpreter's internal values for true and false. This has been found to corrupt the interpreter in some circumstances. Also, these overrides will no longer be possible as of Perl 5.22. Therefore, the -truth import tag is deprecated.

JSON Support

JSON::MaybeXS (or less preferably JSON.pm ) will encode Perl data with boolean.pm values correctly if you use the convert_blessed option:

use JSON::MaybeXS;
use boolean -truth;
my $json = JSON::MaybeXS->new->convert_blessed;
say $json->encode({false => (0 == 1)});     # Says: '{"false":false}',

Author

Ingy döt Net <[email protected]>

Copyright and License

Copyright 2007-2016. Ingy döt Net.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

See http://www.perl.com/perl/misc/Artistic.html

boolean-pm's People

Contributors

dankogai avatar ingydotnet avatar perlpunk avatar shlomif avatar xdg avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.