1*5486feefSafresh1package builtin 0.014; 2256a93a4Safresh1 3256a93a4Safresh1use strict; 4256a93a4Safresh1use warnings; 5256a93a4Safresh1 6*5486feefSafresh1# All code, including &import, is implemented by always-present 7*5486feefSafresh1# functions in the perl interpreter itself. 8256a93a4Safresh1# See also `builtin.c` in perl source 9256a93a4Safresh1 10256a93a4Safresh11; 11256a93a4Safresh1__END__ 12256a93a4Safresh1 13256a93a4Safresh1=head1 NAME 14256a93a4Safresh1 15256a93a4Safresh1builtin - Perl pragma to import built-in utility functions 16256a93a4Safresh1 17256a93a4Safresh1=head1 SYNOPSIS 18256a93a4Safresh1 19256a93a4Safresh1 use builtin qw( 20256a93a4Safresh1 true false is_bool 21*5486feefSafresh1 inf nan 22256a93a4Safresh1 weaken unweaken is_weak 23256a93a4Safresh1 blessed refaddr reftype 24256a93a4Safresh1 created_as_string created_as_number 25*5486feefSafresh1 stringify 26256a93a4Safresh1 ceil floor 27f2a19305Safresh1 indexed 28256a93a4Safresh1 trim 29f2a19305Safresh1 is_tainted 30f2a19305Safresh1 export_lexically 31*5486feefSafresh1 load_module 32256a93a4Safresh1 ); 33256a93a4Safresh1 34*5486feefSafresh1 use builtin ':5.40'; # most of the above 35*5486feefSafresh1 36256a93a4Safresh1=head1 DESCRIPTION 37256a93a4Safresh1 38256a93a4Safresh1Perl provides several utility functions in the C<builtin> package. These are 39256a93a4Safresh1plain functions, and look and behave just like regular user-defined functions 40256a93a4Safresh1do. They do not provide new syntax or require special parsing. These functions 41256a93a4Safresh1are always present in the interpreter and can be called at any time by their 42256a93a4Safresh1fully-qualified names. By default they are not available as short names, but 43256a93a4Safresh1can be requested for convenience. 44256a93a4Safresh1 45256a93a4Safresh1Individual named functions can be imported by listing them as import 46256a93a4Safresh1parameters on the C<use> statement for this pragma. 47256a93a4Safresh1 48*5486feefSafresh1B<Warning>: At present, many of the functions in the C<builtin> namespace are 49*5486feefSafresh1experimental. Calling them will trigger warnings of the 50*5486feefSafresh1C<experimental::builtin> category. 51256a93a4Safresh1 52256a93a4Safresh1=head2 Lexical Import 53256a93a4Safresh1 54256a93a4Safresh1This pragma module creates I<lexical> aliases in the currently-compiling scope 55256a93a4Safresh1to these builtin functions. This is similar to the lexical effect of other 56256a93a4Safresh1pragmas such as L<strict> and L<feature>. 57256a93a4Safresh1 58256a93a4Safresh1 sub classify 59256a93a4Safresh1 { 60256a93a4Safresh1 my $val = shift; 61256a93a4Safresh1 62256a93a4Safresh1 use builtin 'is_bool'; 63256a93a4Safresh1 return is_bool($val) ? "boolean" : "not a boolean"; 64256a93a4Safresh1 } 65256a93a4Safresh1 66256a93a4Safresh1 # the is_bool() function is no longer visible here 67256a93a4Safresh1 # but may still be called by builtin::is_bool() 68256a93a4Safresh1 69256a93a4Safresh1Because these functions are imported lexically, rather than by package 70256a93a4Safresh1symbols, the user does not need to take any special measures to ensure they 71256a93a4Safresh1don't accidentally appear as object methods from a class. 72256a93a4Safresh1 73256a93a4Safresh1 package An::Object::Class { 74256a93a4Safresh1 use builtin 'true', 'false'; 75256a93a4Safresh1 ... 76256a93a4Safresh1 } 77256a93a4Safresh1 78256a93a4Safresh1 # does not appear as a method 79256a93a4Safresh1 An::Object::Class->true; 80256a93a4Safresh1 81256a93a4Safresh1 # Can't locate object method "true" via package "An::Object::Class" 82256a93a4Safresh1 # at ... 83256a93a4Safresh1 84*5486feefSafresh1Once imported, a lexical function is much like any other lexical symbol 85*5486feefSafresh1(such as a variable) in that it cannot be removed again. If you wish to 86*5486feefSafresh1limit the visiblity of an imported C<builtin> function, put it inside its 87*5486feefSafresh1own scope: 88*5486feefSafresh1 89*5486feefSafresh1 { 90*5486feefSafresh1 use builtin 'refaddr'; 91*5486feefSafresh1 ... 92*5486feefSafresh1 } 93*5486feefSafresh1 94*5486feefSafresh1=head2 Version Bundles 95*5486feefSafresh1 96*5486feefSafresh1The entire set of builtin functions that were considered non-experimental by a 97*5486feefSafresh1version of perl can be imported all at once, by requesting a version bundle. 98*5486feefSafresh1This is done by giving the perl release version (without its subversion 99*5486feefSafresh1suffix) after a colon character: 100*5486feefSafresh1 101*5486feefSafresh1 use builtin ':5.40'; 102*5486feefSafresh1 103*5486feefSafresh1The following bundles currently exist: 104*5486feefSafresh1 105*5486feefSafresh1 Version Includes 106*5486feefSafresh1 ------- -------- 107*5486feefSafresh1 108*5486feefSafresh1 :5.40 true false weaken unweaken is_weak blessed refaddr reftype 109*5486feefSafresh1 ceil floor is_tainted trim indexed 110*5486feefSafresh1 111256a93a4Safresh1=head1 FUNCTIONS 112256a93a4Safresh1 113256a93a4Safresh1=head2 true 114256a93a4Safresh1 115256a93a4Safresh1 $val = true; 116256a93a4Safresh1 117256a93a4Safresh1Returns the boolean truth value. While any scalar value can be tested for 118256a93a4Safresh1truth and most defined, non-empty and non-zero values are considered "true" 119256a93a4Safresh1by perl, this one is special in that L</is_bool> considers it to be a 120256a93a4Safresh1distinguished boolean value. 121256a93a4Safresh1 122256a93a4Safresh1This gives an equivalent value to expressions like C<!!1> or C<!0>. 123256a93a4Safresh1 124256a93a4Safresh1=head2 false 125256a93a4Safresh1 126256a93a4Safresh1 $val = false; 127256a93a4Safresh1 128256a93a4Safresh1Returns the boolean fiction value. While any non-true scalar value is 129256a93a4Safresh1considered "false" by perl, this one is special in that L</is_bool> considers 130256a93a4Safresh1it to be a distinguished boolean value. 131256a93a4Safresh1 132256a93a4Safresh1This gives an equivalent value to expressions like C<!!0> or C<!1>. 133256a93a4Safresh1 134256a93a4Safresh1=head2 is_bool 135256a93a4Safresh1 136256a93a4Safresh1 $bool = is_bool($val); 137256a93a4Safresh1 138*5486feefSafresh1This function is currently B<experimental>. 139*5486feefSafresh1 140256a93a4Safresh1Returns true when given a distinguished boolean value, or false if not. A 141256a93a4Safresh1distinguished boolean value is the result of any boolean-returning builtin 142256a93a4Safresh1function (such as C<true> or C<is_bool> itself), boolean-returning operator 143256a93a4Safresh1(such as the C<eq> or C<==> comparison tests or the C<!> negation operator), 144256a93a4Safresh1or any variable containing one of these results. 145256a93a4Safresh1 146256a93a4Safresh1This function used to be named C<isbool>. A compatibility alias is provided 147256a93a4Safresh1currently but will be removed in a later version. 148256a93a4Safresh1 149*5486feefSafresh1=head2 inf 150*5486feefSafresh1 151*5486feefSafresh1 $num = inf; 152*5486feefSafresh1 153*5486feefSafresh1This function is currently B<experimental>. 154*5486feefSafresh1 155*5486feefSafresh1Returns the floating-point infinity value. 156*5486feefSafresh1 157*5486feefSafresh1=head2 nan 158*5486feefSafresh1 159*5486feefSafresh1 $num = nan; 160*5486feefSafresh1 161*5486feefSafresh1This function is currently B<experimental>. 162*5486feefSafresh1 163*5486feefSafresh1Returns the floating-point "Not-a-Number" value. 164*5486feefSafresh1 165256a93a4Safresh1=head2 weaken 166256a93a4Safresh1 167256a93a4Safresh1 weaken($ref); 168256a93a4Safresh1 169256a93a4Safresh1Weakens a reference. A weakened reference does not contribute to the reference 170256a93a4Safresh1count of its referent. If only weakened references to a referent remain, it 171256a93a4Safresh1will be disposed of, and all remaining weak references to it will have their 172256a93a4Safresh1value set to C<undef>. 173256a93a4Safresh1 174256a93a4Safresh1=head2 unweaken 175256a93a4Safresh1 176256a93a4Safresh1 unweaken($ref); 177256a93a4Safresh1 178256a93a4Safresh1Strengthens a reference, undoing the effects of a previous call to L</weaken>. 179256a93a4Safresh1 180256a93a4Safresh1=head2 is_weak 181256a93a4Safresh1 182256a93a4Safresh1 $bool = is_weak($ref); 183256a93a4Safresh1 184256a93a4Safresh1Returns true when given a weakened reference, or false if not a reference or 185256a93a4Safresh1not weak. 186256a93a4Safresh1 187256a93a4Safresh1This function used to be named C<isweak>. A compatibility alias is provided 188256a93a4Safresh1currently but will be removed in a later version. 189256a93a4Safresh1 190256a93a4Safresh1=head2 blessed 191256a93a4Safresh1 192256a93a4Safresh1 $str = blessed($ref); 193256a93a4Safresh1 194256a93a4Safresh1Returns the package name for an object reference, or C<undef> for a 195256a93a4Safresh1non-reference or reference that is not an object. 196256a93a4Safresh1 197256a93a4Safresh1=head2 refaddr 198256a93a4Safresh1 199256a93a4Safresh1 $num = refaddr($ref); 200256a93a4Safresh1 201256a93a4Safresh1Returns the memory address for a reference, or C<undef> for a non-reference. 202256a93a4Safresh1This value is not likely to be very useful for pure Perl code, but is handy as 203256a93a4Safresh1a means to test for referential identity or uniqueness. 204256a93a4Safresh1 205256a93a4Safresh1=head2 reftype 206256a93a4Safresh1 207256a93a4Safresh1 $str = reftype($ref); 208256a93a4Safresh1 209256a93a4Safresh1Returns the basic container type of the referent of a reference, or C<undef> 210256a93a4Safresh1for a non-reference. This is returned as a string in all-capitals, such as 211256a93a4Safresh1C<ARRAY> for array references, or C<HASH> for hash references. 212256a93a4Safresh1 213256a93a4Safresh1=head2 created_as_string 214256a93a4Safresh1 215256a93a4Safresh1 $bool = created_as_string($val); 216256a93a4Safresh1 217*5486feefSafresh1This function is currently B<experimental>. 218*5486feefSafresh1 219256a93a4Safresh1Returns a boolean representing if the argument value was originally created as 220256a93a4Safresh1a string. It will return true for any scalar expression whose most recent 221256a93a4Safresh1assignment or modification was of a string-like nature - such as assignment 222256a93a4Safresh1from a string literal, or the result of a string operation such as 223256a93a4Safresh1concatenation or regexp. It will return false for references (including any 224256a93a4Safresh1object), numbers, booleans and undef. 225256a93a4Safresh1 226256a93a4Safresh1It is unlikely that you will want to use this for regular data validation 227256a93a4Safresh1within Perl, as it will not return true for regular numbers that are still 228256a93a4Safresh1perfectly usable as strings, nor for any object reference - especially objects 229256a93a4Safresh1that overload the stringification operator in an attempt to behave more like 230256a93a4Safresh1strings. For example 231256a93a4Safresh1 232256a93a4Safresh1 my $val = URI->new( "https://metacpan.org/" ); 233256a93a4Safresh1 234256a93a4Safresh1 if( created_as_string $val ) { ... } # this will not execute 235256a93a4Safresh1 236256a93a4Safresh1=head2 created_as_number 237256a93a4Safresh1 238256a93a4Safresh1 $bool = created_as_number($val); 239256a93a4Safresh1 240*5486feefSafresh1This function is currently B<experimental>. 241*5486feefSafresh1 242256a93a4Safresh1Returns a boolean representing if the argument value was originally created as 243256a93a4Safresh1a number. It will return true for any scalar expression whose most recent 244256a93a4Safresh1assignment or modification was of a numerical nature - such as assignment from 245256a93a4Safresh1a number literal, or the result of a numerical operation such as addition. It 246256a93a4Safresh1will return false for references (including any object), strings, booleans and 247256a93a4Safresh1undef. 248256a93a4Safresh1 249256a93a4Safresh1It is unlikely that you will want to use this for regular data validation 250256a93a4Safresh1within Perl, as it will not return true for regular strings of decimal digits 251256a93a4Safresh1that are still perfectly usable as numbers, nor for any object reference - 252256a93a4Safresh1especially objects that overload the numification operator in an attempt to 253256a93a4Safresh1behave more like numbers. For example 254256a93a4Safresh1 255256a93a4Safresh1 my $val = Math::BigInt->new( 123 ); 256256a93a4Safresh1 257256a93a4Safresh1 if( created_as_number $val ) { ... } # this will not execute 258256a93a4Safresh1 259256a93a4Safresh1While most Perl code should operate on scalar values without needing to know 260256a93a4Safresh1their creation history, these two functions are intended to be used by data 261256a93a4Safresh1serialisation modules such as JSON encoders or similar situations, where 262256a93a4Safresh1language interoperability concerns require making a distinction between values 263256a93a4Safresh1that are fundamentally stringlike versus numberlike in nature. 264256a93a4Safresh1 265*5486feefSafresh1=head2 stringify 266*5486feefSafresh1 267*5486feefSafresh1 $str = stringify($val); 268*5486feefSafresh1 269*5486feefSafresh1Returns a new plain perl string that represents the given argument. 270*5486feefSafresh1 271*5486feefSafresh1When given a value that is already a string, a copy of this value is returned 272*5486feefSafresh1unchanged. False booleans are treated like the empty string. 273*5486feefSafresh1 274*5486feefSafresh1Numbers are turned into a decimal representation. True booleans are treated 275*5486feefSafresh1like the number 1. 276*5486feefSafresh1 277*5486feefSafresh1References to objects in classes that have L<overload> and define the C<""> 278*5486feefSafresh1overload entry will use the delegated method to provide a value here. 279*5486feefSafresh1 280*5486feefSafresh1Non-object references, or references to objects in classes without a C<""> 281*5486feefSafresh1overload will return a string that names the underlying container type of 282*5486feefSafresh1the reference, its memory address, and possibly its class name if it is an 283*5486feefSafresh1object. 284*5486feefSafresh1 285256a93a4Safresh1=head2 ceil 286256a93a4Safresh1 287256a93a4Safresh1 $num = ceil($num); 288256a93a4Safresh1 289256a93a4Safresh1Returns the smallest integer value greater than or equal to the given 290256a93a4Safresh1numerical argument. 291256a93a4Safresh1 292256a93a4Safresh1=head2 floor 293256a93a4Safresh1 294256a93a4Safresh1 $num = floor($num); 295256a93a4Safresh1 296256a93a4Safresh1Returns the largest integer value less than or equal to the given numerical 297256a93a4Safresh1argument. 298256a93a4Safresh1 299256a93a4Safresh1=head2 indexed 300256a93a4Safresh1 301256a93a4Safresh1 @ivpairs = indexed(@items) 302256a93a4Safresh1 303256a93a4Safresh1Returns an even-sized list of number/value pairs, where each pair is formed 304256a93a4Safresh1of a number giving an index in the original list followed by the value at that 305256a93a4Safresh1position in it. I.e. returns a list twice the size of the original, being 306256a93a4Safresh1equal to 307256a93a4Safresh1 308256a93a4Safresh1 (0, $items[0], 1, $items[1], 2, $items[2], ...) 309256a93a4Safresh1 310256a93a4Safresh1Note that unlike the core C<values> function, this function returns copies of 311256a93a4Safresh1its original arguments, not aliases to them. Any modifications of these copies 312256a93a4Safresh1are I<not> reflected in modifications to the original. 313256a93a4Safresh1 314256a93a4Safresh1 my @x = ...; 315256a93a4Safresh1 $_++ for indexed @x; # The @x array remains unaffected 316256a93a4Safresh1 317256a93a4Safresh1This function is primarily intended to be useful combined with multi-variable 318256a93a4Safresh1C<foreach> loop syntax; as 319256a93a4Safresh1 320256a93a4Safresh1 foreach my ($index, $value) (indexed LIST) { 321256a93a4Safresh1 ... 322256a93a4Safresh1 } 323256a93a4Safresh1 324256a93a4Safresh1In scalar context this function returns the size of the list that it would 325256a93a4Safresh1otherwise have returned, and provokes a warning in the C<scalar> category. 326256a93a4Safresh1 327256a93a4Safresh1=head2 trim 328256a93a4Safresh1 329256a93a4Safresh1 $stripped = trim($string); 330256a93a4Safresh1 331256a93a4Safresh1Returns the input string with whitespace stripped from the beginning 332256a93a4Safresh1and end. trim() will remove these characters: 333256a93a4Safresh1 334256a93a4Safresh1" ", an ordinary space. 335256a93a4Safresh1 336256a93a4Safresh1"\t", a tab. 337256a93a4Safresh1 338256a93a4Safresh1"\n", a new line (line feed). 339256a93a4Safresh1 340256a93a4Safresh1"\r", a carriage return. 341256a93a4Safresh1 342256a93a4Safresh1and all other Unicode characters that are flagged as whitespace. 343256a93a4Safresh1A complete list is in L<perlrecharclass/Whitespace>. 344256a93a4Safresh1 345256a93a4Safresh1 $var = " Hello world "; # "Hello world" 346256a93a4Safresh1 $var = "\t\t\tHello world"; # "Hello world" 347256a93a4Safresh1 $var = "Hello world\n"; # "Hello world" 348256a93a4Safresh1 $var = "\x{2028}Hello world\x{3000}"; # "Hello world" 349256a93a4Safresh1 350256a93a4Safresh1C<trim> is equivalent to: 351256a93a4Safresh1 352256a93a4Safresh1 $str =~ s/\A\s+|\s+\z//urg; 353256a93a4Safresh1 354256a93a4Safresh1For Perl versions where this feature is not available look at the 355256a93a4Safresh1L<String::Util> module for a comparable implementation. 356256a93a4Safresh1 357f2a19305Safresh1=head2 is_tainted 358f2a19305Safresh1 359f2a19305Safresh1 $bool = is_tainted($var); 360f2a19305Safresh1 361f2a19305Safresh1Returns true when given a tainted variable. 362f2a19305Safresh1 363f2a19305Safresh1=head2 export_lexically 364f2a19305Safresh1 365f2a19305Safresh1 export_lexically($name1, $ref1, $name2, $ref2, ...) 366f2a19305Safresh1 367*5486feefSafresh1This function is currently B<experimental>. 368*5486feefSafresh1 369f2a19305Safresh1Exports new lexical names into the scope currently being compiled. Names given 370f2a19305Safresh1by the first of each pair of values will refer to the corresponding item whose 371f2a19305Safresh1reference is given by the second. Types of item that are permitted are 372f2a19305Safresh1subroutines, and scalar, array, and hash variables. If the item is a 373f2a19305Safresh1subroutine, the name may optionally be prefixed with the C<&> sigil, but for 374f2a19305Safresh1convenience it doesn't have to. For items that are variables the sigil is 375f2a19305Safresh1required, and must match the type of the variable. 376f2a19305Safresh1 377f2a19305Safresh1 export_lexically func => \&func, 378f2a19305Safresh1 '&func' => \&func; # same as above 379f2a19305Safresh1 380f2a19305Safresh1 export_lexically '$scalar' => \my $var; 381f2a19305Safresh1 382f2a19305Safresh1Z<> 383f2a19305Safresh1 384f2a19305Safresh1 # The following are not permitted 385f2a19305Safresh1 export_lexically '$var' => \@arr; # sigil does not match 386f2a19305Safresh1 export_lexically name => \$scalar; # implied '&' sigil does not match 387f2a19305Safresh1 388f2a19305Safresh1 export_lexically '*name' => \*globref; # globrefs are not supported 389f2a19305Safresh1 390f2a19305Safresh1This must be called at compile time; which typically means during a C<BEGIN> 391f2a19305Safresh1block. Usually this would be used as part of an C<import> method of a module, 392f2a19305Safresh1when invoked as part of a C<use ...> statement. 393f2a19305Safresh1 394*5486feefSafresh1=head2 load_module 395*5486feefSafresh1 396*5486feefSafresh1 load_module($module_name); 397*5486feefSafresh1 398*5486feefSafresh1This function is currently B<experimental>. 399*5486feefSafresh1 400*5486feefSafresh1Loads a named module from the inclusion paths (C<@INC>). C<$module_name> must 401*5486feefSafresh1be a string that provides a module name. It cannot be omitted, and providing 402*5486feefSafresh1an invalid module name will result in an exception. Not providing any argument 403*5486feefSafresh1results in a compilation error. Returns the loaded module's name on success. 404*5486feefSafresh1 405*5486feefSafresh1The effect of C<load_module>-ing a module is mostly the same as C<require>-ing, 406*5486feefSafresh1down to the same error conditions when the module does not exist, does not 407*5486feefSafresh1compile, or does not evaluate to a true value. See also 408*5486feefSafresh1L<the C<module_true> feature|feature/"The 'module_true' feature">. 409*5486feefSafresh1 410*5486feefSafresh1C<load_module> can't be used to require a particular version of Perl, nor can 411*5486feefSafresh1it be given a bareword module name as an argument. 412*5486feefSafresh1 413256a93a4Safresh1=head1 SEE ALSO 414256a93a4Safresh1 415256a93a4Safresh1L<perlop>, L<perlfunc>, L<Scalar::Util> 416