where is located the 'module' command?
$> type module
module is a function
module ()
{
eval `/usr/bin/modulecmd bash $*`
}
I guess I have to thank whoever marked my question as lazy. It made me read the whole modules documentation where I found the answer :)
How can I start an interactive console for Perl?
You can use the perl debugger on a trivial program, like so:
perl -de1
Alternatively there's Alexis Sukrieh's Perl Console application, but I haven't used it.
What are the best-practices for implementing a CLI tool in Perl?
As a preface, I spent 3 years engineering and implementing a pretty complicated command line toolset in Perl for a major financial company. The ideas below are basically part of our team's design guidelines.
User Interface
Command line option: allow as many as possible have default values.
NO positional parameters for any command that has more than 2 options.
Have readable options names. If length of command line is a concern for non-interactive calling (e.g. some un-named legacy shells have short limits on command lines), provide short aliases - GetOpt::Long allows that easily.
At the very least, print all options' default values in '-help' message.
Better yet, print all the options' "current" values (e.g. if a parameter and a value are supplied along with "-help", the help message will print parameter's value from command line). That way, people can assemble command line string for complicated command and verify it by appending "-help", before actually running.
Follow Unix standard convention of exiting with non-zero return code if program terminated with errors.
If your program may produce useful (e.g. worth capturing/grepping/whatnot) output, make sure any error/diagnostic messages go to STDERR so they are easily separable.
Ideally, allow the user to specify input/output files via command line parameter, instead of forcing "<" / ">" redirects - this allows MUCH simpler life to people who need to build complicated pipes using your command. Ditto for error messages - have logfile option.
If a command has side effect, having a "whatif/no_post" option is usually a Very Good Idea.
Implementation
As noted previously, don't re-invent the wheel. Use standard command line parameter handling modules - MooseX::Getopt, or Getopt::Long
For Getopt::Long, assign all the parameters to a single hash as opposed to individual variables. Many useful patterns include passing that CLI args hash to object constructors.
Make sure your error messages are clear and informative... E.g. include "$!" in any IO-related error messages. It's worth expending extra 1 minute and 2 lines in your code to have a separate "file not found" vs. "file not readable" errors, as opposed to spending 30 minutes in production emergency because a non-readable file error was misdiagnosed by Production Operations as "No input file" - this is a real life example.
Not really CLI-specific, but validate all parameters, ideally right after getting them.
CLI doesn't allow for a "front-end" validation like webapps do, so be super extra vigilant.As discussed above, modularize business logic. Among other reasons already listed, the amount of times I had to re-implement an existing CLI tool as a web app is vast - and not that difficult if the logic is already a properly designed perm module.
Interesting links
CLI Design Patterns - I think this is ESR's
I will try to add more bullets as I recall them.
How can I export the calling environment correctly into a subshell in Perl?
Check the PATH and modify its content if needed:
use Env qw(@PATH);
# check the PATH:
print join("\n", @PATH);
# modify its content:
push @PATH, "/usr/bin/wpj";
The official manual for this module.
Perl: console / command-line tool for interactive code evaluation and testing
perl -d -e 1
Is perfectly suitable, I've been using it for years and years. But if you just can't,
then you can check out Devel::REPL
Related Topics
Phony Targets for Parallel Execution of Make
How to Decide How Much Stack I Can Use After a Call to Pthread_Attr_Setstacksize
Removing First 3 Characters of File Names in Linux
Linux: Get a Script to Be Able to Ask The User for a File Name Then Open That File
Linux Kconfig Command Line Interface
Why Doesn't ''Var=Value Echo $Var'' Emit Value
Tcp Keepalive - Protocol Not Available
How to Run The Linux/X86/Shell_Bind_Tcp Payload Stand Alone
Fortran: How to Get The Node Name of a Cluster
Succinct Way to Print All Lines Up Until The Last Line That Matches a Given Pattern
Executing Shell Script from Current Directory Without '"./Filename"