|
NAMESystem::Sub - Wrap external command with a DWIM subVERSIONversion 0.162800SYNOPSISuse System::Sub 'hostname'; # Just an example (use Sys::Hostname instead) # Scalar context : returns the first line of the output, without the # line separator my $hostname = hostname; # List context : returns a list of lines without their line separator use System::Sub 'ls'; my @files = ls '-a'; # Process line by line ls -a => sub { push @files, $_[0]; }; use System::Sub 'df' => [ '@ARGV' => [ '-P' ] ]; # -P for POSIX df => sub { return if $. == 1; # Skip the header line # Show the 6th and 5th columns printf "%s: %s\n", (split / +/, $_[0])[5, 4]; }; # Import with options use System::Sub ssh => [ '$0' => '/usr/bin/ssh', '@ARGV' => [ qw< -o RequestTTY=no > ] ]; # Handle exit codes use System::Sub 'zenity'; # a GTK+ dialog display eval { zenity --question => --text => 'How are you today?' => --ok-label => 'Fine!' => --cancel-label => 'Tired.' }; given ($? >> 8) { when (0) { } when (1) { } } # Import with a prototype (see perlsub) use System::Sub 'hostname()'; # Empty prototype: no args allowed use System::Sub hostname => [ '()' => '' ]; # Alternate syntax use strict; # This will fail at compile time with "Too many arguments" hostname("xx"); DESCRIPTIONSee also "System::Sub::AutoLoad" for even simpler usage."System::Sub" provides in your package a sub that wraps the call to an external program. The return value is line(s) dependending on context ("wantarray"). This may be what you need if you want to run external commands as easily as from a Unix shell script but with a perl-ish feel (contextual output). So this is not a universal module for running external programs (like IPC::Run) but instead a simpler interface for a common style of external programs. "System::Sub" may be useful if:
The underlying implementation is currently IPC::Run, but there is no garantee that this will stay that way. IPC::Run works well enough on both Unix and Win32, but it has its own bugs and is very slow. IMPORT OPTIONSOptions can be set for the sub by passing an ARRAY just after the sub name on the "use System::Sub" line.The sigil ("$", "@", "%") is optional.
SUB USAGEArgumentsThe scalar arguments of the sub are directly passed as arguments of the command.The queue of the arguments may contain values of the following type (see "ref" in perlfunc):
Return value(s)
SEE ALSO
TRIVIAI dreamed about such a facility for a long time. I even worked for two years on a ksh framework that I created from scratch just because at the start of the project I didn't dare to bet on Perl because of the lack of readability of the code when most of the work is running other programs.After that project I never really had the need to run the same command in many places of the code, and in many different ways. Until I had the need to wrap Git <http://git-scm.org/> in the release <https://github.com/github-keygen/> script of my github-keygen <https://github.com/github-keygen> project. I wrote the first version of the wrapper there, and quickly extracted it as this module. So, here is it! Last but not least, the pun <https://en.wiktionary.org/wiki/sub-system#English> in the package name is intended. AUTHOROlivier Mengué, "dolmen@cpan.org".CONTRIBUTORSPhilippe Bruhat (BOOK <https://metacpan.org/author/BOOK>).See the Git log <https://github.com/dolmen/p5-System-Sub/commits/master> for details. COPYRIGHT & LICENSECopyright © 2012 Olivier Mengué.This library is free software; you can redistribute it and/or modify it under the same terms as Perl 5 itself.
Visit the GSP FreeBSD Man Page Interface. |