
Modulino: both script and module in Perl - fanf2
https://perlmaven.com/modulino-both-script-and-module
======
rurban
I saw it on brian d foy's website first, around 2010.

[https://web.archive.org/web/20150227080722/http://www.master...](https://web.archive.org/web/20150227080722/http://www.masteringperl.org:80/2013/08/new-
to-modules-as-programs/) is about the module Modulino which allows scripts to
use these tricks.

DrDobbs had it 2004: [http://www.drdobbs.com/scripts-as-
modules/184416165](http://www.drdobbs.com/scripts-as-modules/184416165)

------
SwellJoe
When I came back to Perl after about four years of working primarily in
Python, this was the first question I asked, in the form of "How do I make my
scripts testable as a library while also runnable as a command?"

It wasn't immediately apparent how to do it back then, and it took a little
explaining to even get people on the same page about what I meant. But, it was
a really nice thing in Python that I immediately missed when coming back to
Perl. You don't need to know the word "modulino" to do it, though.

brian d foy first talked about the idea in Perl well over a decade ago but it
was already common in Python (and probably other languages, Python is just
where I saw it first).

------
Twirrim
What is modulino? Attempts to google it keep pulling up blog posts related to
it, but none of them say what it actually is.

~~~
austinjp
New to me too. From the article:

"In the Python world it is quite straight forward to make files work either as
executables or as modules. In Perl it is a bit strange, but doable.

brian d foy has written a number of articles about Modulinos, but in nutshell
a Modulino is a Perl file that can act both as an executable (a script that
you would invoke directly) or as a module (Something you load into memory and
expect to execute code only when you call one of its functions or methods)."

~~~
sigzero
Just to add...one of the big reasons to do this is that testing that script
now becomes much much easier as it is now a "module" and all the testing
facilities in Perl can be brought to bear on it.

------
cafard
Nifty, but something on the page makes Chrome jump down so that one sees only
an ad and contents.

------
tyingq
You could alternatively test $0, but caller() does seem cleaner.

------
falsedan
Note that the

    
    
      use 5.10;
    

puts this article circa 2010

~~~
zbentley
1\. It doesn't say that, it says "use 5.010".

2\. This technique is often used to guarantee _minimum_ version requirements
of a piece of code. No reason to lock your code to the latest version of the
runtime if you know it requires features from an older one.

~~~
jwilk
Right, "use 5.10" would be an error:

    
    
        Perl v5.100.0 required (did you mean v5.10.0?)--this is only v5.26.1, stopped at modulino.pm line 3.
        BEGIN failed--compilation aborted at modulino.pm line 3.

