NAME

Sidef::Module::Func - Functional interface for Perl modules used from Sidef

DESCRIPTION

This class provides a functional (non-OO) bridge between Sidef and Perl modules, enabling Sidef code to call Perl functions exported by a module as if they were methods on an object. It is the underlying mechanism that powers Sidef's frequire() built-in function and the %S module literal syntax.

When a Perl module is loaded with frequire(), the resulting object is an instance of Sidef::Module::Func. Any method call on that object is intercepted by AUTOLOAD and dispatched as a fully-qualified function call into the module's namespace (i.e., Module::Name::function_name(@args)), with arguments and return values transparently converted between Sidef and Perl types.

This is distinct from Sidef::Module::OO, which wraps a Perl object and dispatches method calls on it. Sidef::Module::Func instead calls free functions (package functions) in the module's namespace directly, making it suitable for functional-style Perl modules such as POSIX, List::Util, Scalar::Util, and similar.

SYNOPSIS

Loading a Functional Perl Module

# frequire() returns a Sidef::Module::Func wrapper around the module name
var posix = frequire('POSIX')

say posix.floor(3.7)    # => 3
say posix.ceil(3.2)     # => 4
say posix.fmod(10, 3)   # => 1

Shorthand Syntax

# %S is syntactic sugar for frequire() in the Sidef language
var posix = %S<POSIX>
say posix.floor(4.9)    # => 4

Using List::Util Functions

var lu = frequire('List::Util')

var nums = [3, 1, 4, 1, 5, 9, 2, 6]
say lu.sum(nums...)     # => 31
say lu.min(nums...)     # => 1
say lu.max(nums...)     # => 9

Using Scalar::Util Functions

var su = frequire('Scalar::Util')

var val = 42
say su.looks_like_number(val)   # => 1
say su.blessed(val)             # => (empty / undef)

METHODS

__NEW__

Sidef::Module::Func->__NEW__($module)

Constructs a new Sidef::Module::Func wrapper around the given Perl module name. The module must already be loaded (required) before or during construction.

Parameters:

  • $module - A Perl module name (string), e.g. "POSIX" or "List::Util"

Returns: A new Sidef::Module::Func object

Note: This constructor is used internally by the Sidef runtime. In Sidef code, use frequire() or the %S syntax instead of calling __NEW__ directly.

AUTOLOAD

$func_obj->some_function(@args)

All method calls on a Sidef::Module::Func object are intercepted by AUTOLOAD and forwarded as function calls into the wrapped module's namespace. For example, calling $obj->floor(3.7) on a Sidef::Module::Func wrapping "POSIX" results in POSIX::floor(3.7) being called.

Arguments are automatically converted from Sidef types to plain Perl values before the call, and return values are automatically converted back to Sidef types using Sidef::Types::Perl::Perl->to_sidef.

Argument Conversion:

  • Sidef objects (Sidef::*) → the underlying Perl value via get_value

  • Plain Perl values → passed through unchanged

Return Value Conversion:

  • Perl arrays → Sidef::Types::Array::Array

  • Perl hashes → Sidef::Types::Hash::Hash

  • Perl code refs → Sidef::Types::Block::Block

  • Numeric scalars → Sidef::Types::Number::Number

  • String scalars → Sidef::Types::String::String

  • Blessed Perl objects → Sidef::Module::OO

  • Multiple return values → Sidef::Types::Array::Array in scalar context, individual Sidef values in list context

Example:

var lu = frequire('List::Util')
var total = lu.sum(1, 2, 3, 4, 5)
say total   # => 15

ADVANCED EXAMPLES

Working with POSIX Functions

var posix = frequire('POSIX')

say posix.floor(2.9)        # => 2
say posix.ceil(2.1)         # => 3
say posix.round(2.5)        # => 3
say posix.fabs(-3.14)       # => 3.14
say posix.pow(2, 10)        # => 1024

Using List::Util Reductions

var lu = frequire('List::Util')

var words = ['banana', 'apple', 'cherry']
var longest = lu.reduce({ |a, b| a.len > b.len ? a : b }, words...)
say longest     # => banana

say lu.any({ _ > 5 }, 1, 3, 7, 2)   # => true
say lu.all({ _ > 0 }, 1, 2, 3)       # => true
say lu.none({ _ < 0 }, 1, 2, 3)      # => true

Calling Module Functions with Sidef Values

# Sidef values are automatically converted to their underlying Perl representations
var su = frequire('Scalar::Util')
var num = 3.14
say su.looks_like_number(num)   # => 1

Combining frequire with require

# Load a functional module and an OO module together
var list_util = frequire('List::Util')
var file_spec = require('File::Spec')

var values = [5, 2, 8, 1, 9, 3]
say list_util.max(values...)    # => 9
say list_util.min(values...)    # => 1
say file_spec.catfile(file_spec.tmpdir, 'sidef.txt')

Using Data::Dumper for Debugging

var dd = frequire('Data::Dumper')
var data = [1, [2, 3], {key => 'value'}]
say dd.Dumper(data)

SEE ALSO