BSON::Types - Helper functions to wrap BSON type classes
| Use Case | Command | Description |
|---|---|---|
| Create 32βbit integer | bson_int32(42) | Wraps integer as BSON Int32 |
| Create double | bson_double(3.14) | Wraps float as BSON Double |
| Create Object ID | bson_oid() | Generates a new unique 12βbyte Object ID |
| Create current time | bson_time() | Returns current UTC time (ms precision) |
| Wrap raw BSON | bson_raw($bson) | Wraps alreadyβencoded BSON document |
| Create boolean | bson_bool($expr) | Returns BSON boolean (prefer boolean::boolean()) |
version v1.12.2
use BSON::Types ':all';
$int32 = bson_int32(42);
$double = bson_double(3.14159);
$decimal = bson_decimal("24.01");
$time = bson_time(); # now
...
This module provides helper functions for BSON type wrappers. Type wrappers use objects corresponding to BSON types to represent data that would have ambiguous type or don't have a native Perl representation
For example, because Perl scalars can represent strings, integers or floating point numbers, the serialization rules depend on various heuristics. By wrapping a Perl scalar with a class, such as BSON::Int32, users can specify exactly how a scalar should serialize to BSON.
bson_bytes( $byte_string [, $subtype] ) β This function returns a BSON::Bytes object wrapping the provided string. A numeric subtype may be provided as a second argument, but this is not recommended for new applications.bson_code( $javascript [, $hashref] ) β This function returns a BSON::Code object wrapping the provided Javascript code. An optional hashref representing variables in scope for the function may be given as well.bson_dbref( $object_id, $collection_name ) β This function returns a BSON::DBRef object wrapping the provided Object ID and collection name.bson_decimal128( "0.12" ) β This function returns a BSON::Decimal128 object wrapping the provided decimal string. Unlike floating point values, this preserves exact decimal precision.bson_doc( first => "hello, second => "world" ) β This function returns a BSON::Doc object, which preserves the order of the provided key-value pairs.bson_array(...) β This function returns a BSON::Array object, which preserves the order of the provided list of elements.bson_double( 1.0 ) β This function returns a BSON::Double object wrapping a native double value. This ensures it serializes to BSON as a double rather than a string or integer given Perl's lax typing for scalars.bson_int32( 42 ) β This function returns a BSON::Int32 object wrapping a native integer value. This ensures it serializes to BSON as an Int32 rather than a string or double given Perl's lax typing for scalars.bson_int64( 0 ) β This function returns a BSON::Int64 object, wrapping a native integer value. This ensures it serializes to BSON as an Int64 rather than a string or double given Perl's lax typing for scalars.bson_maxkey() β This function returns a singleton representing the "maximum key" BSON type.bson_minkey() β This function returns a singleton representing the "minimum key" BSON type.bson_oid() β No arguments: generates a new 12βbyte Object ID.bson_oid( $bytes ) β From 12βbyte packed OID.bson_oid( $hex ) β From 24 hex characters.bson_raw( $bson_encoded ) β This function returns a BSON::Raw object wrapping an already BSON-encoded document.bson_regex( $pattern [, $flags] ) β This function returns a BSON::Regex object wrapping a PCRE pattern and optional flags.bson_string( "08544" ) β This function returns a BSON::String object, wrapping a native string value. This ensures it serializes to BSON as a UTFβ8 string rather than an integer or double given Perl's lax typing for scalars.bson_time( $seconds_from_epoch ) β This function returns a BSON::Time object representing a UTC date and time to millisecond precision. The argument must be given as a number of seconds relative to the Unix epoch (positive or negative). The number may be a floating point value for fractional seconds. If no argument is provided, the current time from Time::HiRes is used.bson_timestamp( $seconds_from_epoch, $increment ) β This function returns a BSON::Timestamp object. It is not recommended for general use.bson_bool( $expression ) (DISCOURAGED) β This function returns a boolean object (true or false) based on the provided expression (or false if no expression is provided). It is provided for consistency so that all BSON types have a corresponding helper function. For efficiency, use boolean::boolean() directly, instead.This software is Copyright (c) 2020 by Stefan G. and MongoDB, Inc.
This is free software, licensed under:
The Apache License, Version 2.0, January 2004
Generated by phpman v4.9.25-4-g0d844aa · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-07-07 09:27 @216.73.217.93
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Enhanced by LLM: deepseek-v4-pro / taotoken.net / www.chedong.com - original format