{"id":598,"date":"2015-10-03T14:13:41","date_gmt":"2015-10-03T19:13:41","guid":{"rendered":"http:\/\/www.aibistin.com\/?p=598"},"modified":"2015-10-03T16:34:21","modified_gmt":"2015-10-03T21:34:21","slug":"using-mooxoptions-to-run-a-perl-script","status":"publish","type":"post","link":"https:\/\/www.aibistin.com\/?p=598","title":{"rendered":"Using MooX::Options to run a Perl script."},"content":{"rendered":"<p>In my last post about\u00a0<a href=\"http:\/\/www.aibistin.com\/?p=581\">using Moo<\/a>\u00a0I added some more functionality to my File::Info package. I added some more attributes and some Moo roles. I also updated and ran my test script to ensure that it worked well.<\/p>\n<p>Now I want to put my File::Info class to use. \u00a0Normally when I write scripts I like to use configuration files with\u00a0<a href=\"https:\/\/metacpan.org\/pod\/Config::General\">Config::General<\/a>\u00a0and command line options (CLI) with\u00a0<a href=\"https:\/\/metacpan.org\/pod\/Getopt::Long\">Getopt::Long<\/a>. These have served me very well in the past. Having seen a post recently on the excellent <a href=\"http:\/\/perlmaven.com\/\">Perl Maven<\/a>\u00a0site about\u00a0<a href=\"http:\/\/perlmaven.com\/command-line-scripts-with-moo\">Command Line Scripts With Moo<\/a>\u00a0I decided to give\u00a0<a href=\"https:\/\/metacpan.org\/pod\/MooX::Options\">MooX::Options<\/a>\u00a0a try.<\/p>\n<p>This script accepts one input option, \u00a0&#8216;in_file&#8217;. The MooX option attribute takes care of \u00a0most of the validation and error handling of the input. It also takes care of displaying &#8216;&#8211;help&#8217; and &#8216;&#8211;man&#8217; \u00a0documentation based on what is entered into the &#8216;doc&#8217; and &#8216;long_doc&#8217; option parameters respectively.<\/p>\n<p>You can even do coercion on the option input just as you would with any Moo attribute. Here I could have coerced the input file into a &#8216;Path::Tiny&#8217; object if I wished. However this is already taken care of in the File::Info module already.<\/p>\n<p>Even though this is a script, because it uses Moo, you could also create Moo attributes. It really is a neat way to write Perl scripts with input options.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:perl decode:true \" title=\"file_info.pl Script\">use Moo;\r\nuse MooX::Options;\r\nuse v5.16;\r\nuse FindBin qw\/$Bin\/;\r\nuse lib qq{$Bin\/..\/lib};\r\n\r\nuse File::Info;\r\n\r\n#-------------------------------------------------------------------------------\r\n#  Options\r\n#-------------------------------------------------------------------------------\r\n\r\noption 'in_file' =&gt; (\r\n    is       =&gt; 'ro',\r\n    format   =&gt; 's',\r\n    required =&gt; 1,\r\n    short =&gt; q{file|i_f},\r\n    doc      =&gt; q{The file you wish to examine.},\r\n    long_doc =&gt; q{\r\n    The full file path of the file that you would like to get size and age information about.},\r\n);\r\n\r\n#-------------------------------------------------------------------------------\r\n#  Functions\r\n#-------------------------------------------------------------------------------\r\nsub run_like_the_wind {\r\n    my ($self) = @_;\r\n    say qq{\\n};\r\n    say q{Your File: } . $self-&gt;in_file;\r\n    say q{=} x (length($self-&gt;in_file) + 11);\r\n\r\n    my $file_info_obj = File::Info-&gt;new( file =&gt; $self-&gt;in_file );\r\n\r\n    say qq{Not so pretty...\\n}\r\n      . $file_info_obj-&gt;file-&gt;stringify . q{ size is }\r\n      . $file_info_obj-&gt;size_bytes\r\n      . q{ and it's been }\r\n      . $file_info_obj-&gt;seconds_since_mod\r\n      . qq{ seconds since its last modification.};\r\n\r\n    say qq{\\nA little prettier...\\n}\r\n      . $file_info_obj-&gt;file-&gt;stringify . q{ size is }\r\n      . $file_info_obj-&gt;make_file_size_pretty\r\n      . q{ and was last modified on }\r\n      . $file_info_obj-&gt;mod_time_moment-&gt;strftime(qq{%a %b %e at %I:%M:%S %p})  . q{ local time.};\r\n\r\n     say q{That's } .$file_info_obj-&gt;time_since_mod_pretty  . qq{ ago!\\n};\r\n}\r\n\r\n#--- Run the script\r\nmain-&gt;new_with_options-&gt;run_like_the_wind;\r\n<\/pre>\n<p>On the first run I forget to enter the &#8216;in_file&#8217;.<\/p>\n<pre class=\"theme:solarized-dark nums:false nums-toggle:false lang:default decode:true\" title=\"Test run without input file. \">Moo &gt; perl bin\/file_info.pl \r\nin_file is missing\r\nUSAGE: file_info.pl [-h] [long options...]\r\n\r\n    --in_file: String\r\n        The file you wish to examine.\r\n\r\n    \r\n    --usage:\r\n        show a short help message\r\n\r\n    \r\n    -h --help:\r\n        show a help message\r\n\r\n    \r\n    --man:\r\n        show the manual\r\n\r\n    \r\n[14:44 - 0.62]\r\nMoo &gt; \r\n<\/pre>\n<p>It prints a nice error message with some instructions. \u00a0Next time I will get it right.<\/p>\n<pre class=\"theme:solarized-dark nums:false nums-toggle:false lang:default decode:true \" title=\"Run file_info.pl with an input file. \">Moo &gt; perl bin\/file_info.pl --in_file IMAG0029.jpg \r\n\r\n\r\nYour File: IMAG0029.jpg\r\n=======================\r\nNot so pretty...\r\nIMAG0029.jpg size is 592023 and it's been 78620118 seconds since its last modification.\r\n\r\nA little prettier...\r\nIMAG0029.jpg size is 578.15 KB and was last modified on Sat Apr  6 at 04:02:28 PM local time.\r\nThat's 129.99 Weeks ago!\r\n\r\n[14:57 - 0.04]\r\n[austin@the-general-II 83] Moo &gt; \r\n<\/pre>\n<p>There is a lot more that could be added to our File::Info module. It could be subclassed and or given some more Roles to provide some extra functionality.<\/p>\n<p>Here is some more useful links on this topic.<\/p>\n<p><a href=\"https:\/\/metacpan.org\/pod\/MooX::Options\">MooX::Options on CPAN<\/a><\/p>\n<p><a href=\"http:\/\/perladvent.org\/2014\/2014-12-13.html\">Now I Have Better Options, by Mark Fowler<\/a>\u00a0in the Perl Advent Calendar<\/p>\n<p><a href=\"https:\/\/github.com\/rehsack\/App-Math-Tutor\">App::Math::Tutor, by Jens Rehsack<\/a>\u00a0, has lots of Moo and Moox::Options examples.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my last post about\u00a0using Moo\u00a0I added some more functionality to my File::Info package. I added some more attributes and some Moo roles. I also updated and ran my test script to ensure that it worked well. Now I want to put my File::Info class to use. \u00a0Normally when I write scripts I like to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[29,35,20],"class_list":["post-598","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-moo","tag-mooxoptions","tag-perl"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.aibistin.com\/index.php?rest_route=\/wp\/v2\/posts\/598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aibistin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aibistin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aibistin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aibistin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=598"}],"version-history":[{"count":9,"href":"https:\/\/www.aibistin.com\/index.php?rest_route=\/wp\/v2\/posts\/598\/revisions"}],"predecessor-version":[{"id":609,"href":"https:\/\/www.aibistin.com\/index.php?rest_route=\/wp\/v2\/posts\/598\/revisions\/609"}],"wp:attachment":[{"href":"https:\/\/www.aibistin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aibistin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aibistin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}