The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Sidef::Module::Require

DESCRIPTION

This module can load a perl module at run-time, via the keyword require.

SYNOPSIS

    var req = (require "My::Perl::Module");

    if (nil != req) {   # module successfully loaded
        var result = (req.some_method);
    }

METHODS

frequire

Require.frequire() -> Obj

Return the

require

require(String) -> Sidef::Module::Caller

  • Loading Object-Oriented modules:

        var req = (require "Some::Perl::Module");

    When the Perl module is successfully loaded, it returns a Sidef::Module::Caller object, which can call any method from the required module.

        var oo_mod = (req.new);
        var result = (oo_mod.some_method);
        var solved = (oo_mod.solve(1, 4));
  • Loading Functional-oriented modules:

    To initialize a functional-oriented module, apply an empty method ('->()') on the object returned by the require keyword.

        var func_mod = (require "Some::Perl::Module"->());     # notice the '->()'
    
        var result = (func_mod->some_function(x,y));
        var solved = (func_mod->solve(1, 4));
  • Real object-oriented module loading example:

        var req = (require 'LWP::UserAgent');
    
        if (nil != req) {   # if the module is successfully loaded
    
            var lwp = (req.new(
                    'show_progress' => 1,
                    'timeout'       => 10,
                    'agent'         => 'Mozilla/5.0',
            ));
    
            var resp = (lwp->get("http://google.com"));
    
            if (Bool.new(resp->is_success)) {
                var content = (Str.new(resp->content));
                # ... do something with the content variable ...
            }
            else {
                "[ERROR] Response: %s\n".sprintf(Str.new(resp->status_line)).warn;
            }
        }
  • Real functional-oriented module loading example:

        var spec = ( require 'File::Spec::Functions'->() );
    
        # Calling the curdir() function
        var curdir = (Dir.new(spec.curdir));
    
        # Call the catfile() function
        var file   = (File.new(spec.catfile("my", "dir", "file.ext")));
    
        ## On Unix, file is 'my/dir/file.ext'