# man > BSON::Types

---
type: CommandReference
command: BSON::Types
mode: perldoc
section: 3pm
source: perldoc
---

## Quick Reference
- `bson_int32(42)` — wrap an integer as a BSON Int32
- `bson_double(3.14159)` — wrap a double-precision float
- `bson_decimal128("24.01")` — wrap a decimal string with exact precision
- `bson_time()` — current UTC time with millisecond precision
- `bson_oid()` — generate a new MongoDB Object ID
- `bson_bytes("data")` — wrap a byte string
- `bson_doc(key => "value")` — create an order‑preserving document
- `bson_regex($pattern, "i")` — wrap a PCRE regex with flags

## Name
Helper functions to construct BSON type wrapper objects for unambiguous BSON serialization.

## Synopsis
perl
use BSON::Types ':all';

$int32   = bson_int32(42);
$double  = bson_double(3.14159);
$decimal = bson_decimal128("24.01");
$time    = bson_time();            # current time
$oid     = bson_oid();            # new Object ID
$bytes   = bson_bytes("\xff\xfe");
$regex   = bson_regex($pattern, "is");
$doc     = bson_doc( first => "hello", second => [1,2,3] );
...
## Functions
All functions return objects from the `BSON::*` type family.  
Trailing digits indicate the BSON type (e.g. `Int32` → 32-bit integer).

- **`bson_bytes($string, $subtype?)`** → [`BSON::Bytes`](https://metacpan.org/pod/BSON::Bytes)  
  Wraps a byte string. An optional numeric subtype may be given; this is **not recommended** for new code.

- **`bson_code($javascript, $scope_hashref?)`** → [`BSON::Code`](https://metacpan.org/pod/BSON::Code)  
  Wraps JavaScript code. An optional hashref defines scope variables.

- **`bson_dbref($oid, $collection)`** → [`BSON::DBRef`](https://metacpan.org/pod/BSON::DBRef)  
  Wraps an Object ID and a collection name for a DBRef.

- **`bson_decimal128($string)`** → [`BSON::Decimal128`](https://metacpan.org/pod/BSON::Decimal128)  
  Wraps a decimal number given as a **string**. Preserves exact decimal precision (unlike floating point). Example: `"1.23456789101112131415116E-412"`.

- **`bson_doc(@key_value_pairs)`** → [`BSON::Doc`](https://metacpan.org/pod/BSON::Doc)  
  Creates an order‑preserving document from the supplied key/value pairs.

- **`bson_array(@elements)`** → [`BSON::Array`](https://metacpan.org/pod/BSON::Array)  
  Creates an order‑preserving BSON array.

- **`bson_double($number)`** → [`BSON::Double`](https://metacpan.org/pod/BSON::Double)  
  Wraps a native double value, forcing BSON serialization as a 64‑bit float.

- **`bson_int32($integer)`** → [`BSON::Int32`](https://metacpan.org/pod/BSON::Int32)  
  Wraps a native integer as a 32‑bit BSON integer.

- **`bson_int64($integer)`** → [`BSON::Int64`](https://metacpan.org/pod/BSON::Int64)  
  Wraps a native integer as a 64‑bit BSON integer.

- **`bson_maxkey()`** → singleton  
  Returns the BSON “maximum key” sentinel.

- **`bson_minkey()`** → singleton  
  Returns the BSON “minimum key” sentinel.

- **`bson_oid($value?)`** → [`BSON::OID`](https://metacpan.org/pod/BSON::OID)  
  Without arguments: generates a new, unique 12‑byte Object ID.  
  With a 12‑byte packed binary string: wraps it directly.  
  With a 24‑character hex string: packs it to binary first.

- **`bson_raw($bson_encoded_data)`** → [`BSON::Raw`](https://metacpan.org/pod/BSON::Raw)  
  Wraps an already‑encoded BSON document for passthrough.

- **`bson_regex($pattern, $flags?)`** → [`BSON::Regex`](https://metacpan.org/pod/BSON::Regex)  
  Wraps a PCRE pattern and optional flags (e.g. `"imsx"`).

- **`bson_string($string)`** → [`BSON::String`](https://metacpan.org/pod/BSON::String)  
  Wraps a native string, guaranteeing BSON serialization as a UTF‑8 string.

- **`bson_time($seconds_from_epoch?)`** → [`BSON::Time`](https://metacpan.org/pod/BSON::Time)  
  Wraps a UTC timestamp with millisecond precision. Accepts seconds since Unix epoch (including fractional parts). If omitted, the current time is taken from [`Time::HiRes`](https://metacpan.org/pod/Time::HiRes).

- **`bson_timestamp($seconds, $increment)`** → [`BSON::Timestamp`](https://metacpan.org/pod/BSON::Timestamp)  
  Wraps a BSON timestamp (oplog‑style). **Not recommended** for general use.

- **`bson_bool($expression)`** (**deprecated**)  
  Returns a boolean `true` or `false` based on `$expression`.  
  Prefer `boolean::boolean($expression)` for efficiency (see [boolean](https://metacpan.org/pod/boolean)).

## Examples
perl
use BSON::Types ':all';

# force 32-bit integer
my $val = bson_int32(42);

# force double
my $pi  = bson_double(3.14159265358979);

# decimal128 for monetary amounts
my $price = bson_decimal128("9.99");

# time now
my $ts = bson_time();

# time from epoch
my $epoch = bson_time(1597408800.123);

# new Object ID
my $oid1 = bson_oid();
my $oid2 = bson_oid("507f1f77bcf86cd799439011");  # from hex

# regex with flags
my $re = bson_regex("^foo", "i");

# ordered document
my $doc = bson_doc( name => "Alice", age => bson_int32(30) );
## See Also
- [`BSON`](https://metacpan.org/pod/BSON)
- [`BSON::Bytes`](https://metacpan.org/pod/BSON::Bytes)
- [`BSON::Doc`](https://metacpan.org/pod/BSON::Doc)
- [`BSON::Double`](https://metacpan.org/pod/BSON::Double)
- [`BSON::Int32`](https://metacpan.org/pod/BSON::Int32)
- [`BSON::Int64`](https://metacpan.org/pod/BSON::Int64)
- [`BSON::Decimal128`](https://metacpan.org/pod/BSON::Decimal128)
- [`BSON::OID`](https://metacpan.org/pod/BSON::OID)
- [`BSON::Regex`](https://metacpan.org/pod/BSON::Regex)
- [`BSON::String`](https://metacpan.org/pod/BSON::String)
- [`BSON::Time`](https://metacpan.org/pod/BSON::Time)
- [BSON specification](https://bsonspec.org/)