{"id":42,"date":"2009-08-04T09:00:47","date_gmt":"2009-08-04T16:00:47","guid":{"rendered":"http:\/\/www.imaginarybillboards.com\/?p=42"},"modified":"2009-08-10T15:26:41","modified_gmt":"2009-08-10T22:26:41","slug":"how-i-pass-parameters-to-my-programs-in-perl","status":"publish","type":"post","link":"http:\/\/www.imaginarybillboards.com\/?p=42","title":{"rendered":"How I pass parameters to my programs in perl"},"content":{"rendered":"

I often want to change what a program does with parameters. \u00c2\u00a0Anything from changing a date (very very common) to a log location, to email addresses, etc. \u00c2\u00a0Combined with another favorite unix command line trick, it’s super easy.<\/p>\n

Just a couple of notes here – I don’t like the usual unix-style for some reason. \u00c2\u00a0I’m not going to remember that -d is debug, and that -a <email address> is email address. \u00c2\u00a0Plus, one of the problems with programmers is that the first way we learn to do something is the way we’ll do it forever. \u00c2\u00a0When I started with cgi, we did the full “my $cgi = new CGI; my $thing=$cgi->param(‘thing’)”. \u00c2\u00a0You *can* do \u00c2\u00a0just the standara param(‘thing’) but then you have to use “use CGI qw\/:standard\/;” which I can never remember. \u00c2\u00a0No big deal either way.<\/p>\n

So here goes. \u00c2\u00a0For this one, I have a program that gets a bunch of stuff from a bunch of machines and does some manipulation of the data. \u00c2\u00a0I want to be able to limit the machines it goes to, whether or not debug is on, how many threads, how many concurrent inserts, where it logs, and the date it runs for. \u00c2\u00a0(assuming it gets the list of machines from a DB if not specified).<\/p>\n

perl loader.pl host=all_hosts threads=10 inserts=15000 logfile=~\/logs\/`date +\\%y-\\%m-\\%d` debug=0 date=`date +\\%y-\\%m-\\%d`<\/p>\n

And the code that runs:<\/p>\n

#!\/usr\/bin\/env  perl\r\nuse CGI;\r\nmy $cgi = new CGI;\r\nmy $date = $cgi->param('date') || `date +\\%y-\\%m-\\%d`; chomp($date); #gives a default, with no newline\r\nmy $debug = $cgi->param('debug') || 0;\r\nmy $host = $cgi->param('host') || usage(); \u00c2\u00a0# you have to pass at least one!\r\nmy @hosts = $host eq 'all_hosts' ? all_hosts() : split(',',$host);   #ternary test, gets a list, depending on what's passed\r\nmy $threads = $cgi->param('threads') || 10;\r\nmy $inserts = $cgi->param('inserts') || 15000;\r\nmy $logfile = $cgi->param('logfile') || \"~\/logs\/$date\";\r\n\r\n#do something\r\nforeach my $hostname(@hosts)\r\n{\r\n  #get some data and insert it, log, etc.\r\n}\r\n\r\nsub usage()\r\n{\r\n  print \"Usage: perl loader.pl host= [threads=10 inserts=15000 logfile=<logfile> debug=1 date=<YYYY-MM-DD>]\\n\";\r\n  exit();\r\n}<\/pre>\n

While somewhat on the subject, that date hack is a great one to remember. A lot of the time you want to be explicit about the date to run for. Alternatively, I have a lot of programs I want to run for yesterday. It’s almost as simple.
\n
\n`date +\\%Y-\\%m-\\%d --date='1 days ago'`
\n<\/code>
\nThe above line returns the date in YYYY-MM-DD format for yesterday. That’s it! Put it into a cron job the next morning for yesterday and look cool to those windows guys.<\/p>\n","protected":false},"excerpt":{"rendered":"

I often want to change what a program does with parameters. \u00c2\u00a0Anything from changing a date (very very common) to a log location, to email addresses, etc. \u00c2\u00a0Combined with another favorite unix command line trick, it’s super easy. Just a couple of notes here – I don’t like the usual unix-style for some reason. \u00c2\u00a0I’m […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15],"tags":[10],"_links":{"self":[{"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=\/wp\/v2\/posts\/42"}],"collection":[{"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=42"}],"version-history":[{"count":4,"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=\/wp\/v2\/posts\/42\/revisions"}],"predecessor-version":[{"id":46,"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=\/wp\/v2\/posts\/42\/revisions\/46"}],"wp:attachment":[{"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.imaginarybillboards.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}