Questions tagged [modulino]

A modulino is code that can be run as a script or loaded as a module (or as a library). Thus functionality that is created in developing one script can be easily used in other scripts later.

A modulino is code that can be run as a script or loaded as a module (or as a library). Thus functionality that is created in developing one script can be easily used in other scripts later.

Originally a Perl design pattern, the term modulino was coined by brian d foy at least by 2004 when he wrote about it in Dr. Drobbs Journal. (see also Makefile.PL as a modulino).

The key to creating a modulino is that the code has to determine whether it is being used as a script or as a module.

Language examples (on Stack Overflow):

15 questions
11
votes
3 answers

In Perl, how can I find out if my file is being used as a module or run as a script?

Let's say I have a Perl file in which there are parts I need to run only when I'm called as a script. I remember reading sometime back about including those parts in a main() method and doing a main() unless(
Sundar R
  • 13,776
  • 6
  • 49
  • 76
8
votes
1 answer

Perl -d and modulino issue

I have some scripts that I have started unit-testing using the "modulino" idea. I have encountered a problem in that when the script is called with "perl -d" the script does not run as caller() returns a true value. I have the main body of the…
LanceW
  • 81
  • 2
7
votes
1 answer

How do I make a modulino in Perl6?

I want to make a modulino (a file that can run as either a module or a script) in Perl6. The following code "processes" file names from the command line: sub MAIN ( *@filenames ) { for @filenames -> $filename { say "Processing…
Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
6
votes
1 answer

Should Raku run MAIN if the file is required?

Here's a short Raku program that declare a MAIN subroutine. I should only see output if I execute the program directly: $ cat main.rakumod sub MAIN { say "Called as a program!" } And I see output when I execute the program directly: $ raku…
brian d foy
  • 129,424
  • 31
  • 207
  • 592
6
votes
4 answers

How to include unit tests in a ruby module?

I'm trying to include the unit tests for a module in the same source file as the module itself, following the Perl modulino model. #! /usr/bin/env ruby require 'test/unit' module Modulino def modulino_function return 0 …
philant
  • 34,748
  • 11
  • 69
  • 112
3
votes
1 answer

How can I debug a single Perl unit test sub?

I have the usual MakeMaker module with a t/ tests directory, and I can run a single test file with e.g., prove -I lib t/my-test.t. My tests use Test::Class and Test::More and subs (with the modulino technique from Effective Perl), like this: use…
Robert
  • 7,394
  • 40
  • 45
  • 64
3
votes
1 answer

Why does 'package' keyword in Perl modulino break tests?

Let's say there's a simple Perl script testme.pl like this: use strict; use warnings; sub testme { return 1; } 1; And a test file testme.t like this: use strict; use warnings; use…
Richlv
  • 3,954
  • 1
  • 17
  • 21
3
votes
3 answers

Equivalent to Perl Modulino for Ruby, Python?

I know Perl has a design pattern known as a modulino, in which a library module file can act as both a library and a script. Is there any equivalent to this in Ruby / Python? I think this design pattern would be very useful for me; I'm writing…
codesw1tch
  • 720
  • 10
  • 29
2
votes
3 answers

Load and use perl module without knowing the module name but knowing the file name

I have been toying with the modulino perl pattern and would like to load one without knowing the package name and only the file in which it is. I'm looking for something that could be used like this: eval { my $file =…
Ricardo Marimon
  • 10,339
  • 9
  • 52
  • 59
2
votes
3 answers

How can I run a test perl script designed for prove form inside TextMate?

I'm using TextMate 1.5.10 (Mac OSX 10.7.2) to write a perl modulino application. To verify the functionality, I'm using test scripts designed to be run with the prove command line tool. An example of the directory structure I'm using looks like…
Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
1
vote
1 answer

How should I kill a perl test script if the module to test isn't available?

I'm building a perl application as a "modulino" to make testing easier. The directory structure looks something like this: MainScript.pm t/001_load.t t/002_setup.t t/003_etc... The first test in each .t test file also loads the module with: BEGIN…
Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
1
vote
1 answer

Accessing a global in a perl modulino from a test

In a unit test I need to set a global variable used in a perl script that I have changed into a modulino. I'm calling subs in the modulino quite happily. Using perl (v5.18.2) built for x86_64-linux-gnu-thread-multi, on Ubuntu. Note the modulino is…
1
vote
2 answers

Modulino package name added to @_

Consider the following basic Perl modulino: #!/usr/bin/perl -l package Toto; __PACKAGE__->run(@ARGV) unless caller(); sub run { print "@ARGV"; print "@_"; } 1; If I run it on the command line, I get: $ ./Toto.pm 1 2 3 1 2 3 Toto 1 2 3 If I…
Philippe A.
  • 2,885
  • 2
  • 28
  • 37
1
vote
2 answers

Set perl cgi query_string offline

I'd like to write some unit tests for my cgi script. I wrote my script as a modulino (script which could be a module depending on context) and I would like to test its functionality but also make sure that all parameters in query_string are set as…
1
vote
1 answer

How to write a modulino in zsh?

I like to write my scripts as "modulinos", shell scripts that can be called by themselves as little CLI programs, or imported by other programs as libraries. I can do this in bash with: https://github.com/mcandre/scriptedmain/tree/master/bash And I…
mcandre
  • 22,868
  • 20
  • 88
  • 147