{"id":9451,"date":"2005-09-14T16:35:12","date_gmt":"2005-09-14T13:35:12","guid":{"rendered":"https:\/\/mamchenkov.net\/wordpress\/2005\/09\/14\/working-with-named-pipes-in-perl\/"},"modified":"2005-09-14T16:35:12","modified_gmt":"2005-09-14T13:35:12","slug":"working-with-named-pipes-in-perl","status":"publish","type":"post","link":"https:\/\/mamchenkov.net\/wordpress\/2005\/09\/14\/working-with-named-pipes-in-perl\/","title":{"rendered":"Working with named pipes in Perl"},"content":{"rendered":"<!-- google_ad_section_start -->\n<p>The collegue of mine came across a problem that developed into an interesting solution that I decided to share with the world.  Actually, I think the world is pretty much aware of the solution, but just in case that I will ever be looking for this solution again, I&#8217;ll have it handy here.<\/p>\n<p>The task at hand was to do some processing of the logs on the fly.  The <code>syslog<\/code> was configured to filter the appropriate logs into a named pipe and a Perl script was written to read from the said pipe and do all the processing.<\/p>\n<p>The original piece of code looked something like this:<\/p>\n<pre>\r\nopen (SYSLOG, \"<$named_pipe\") \r\n  or die \"Couldn't open $named_pipe: $!\\n\";\r\n\r\nwhile (<SYSLOG>) {\r\n  do_processing($_);\r\n}\r\n\r\nclose(SYSLOG);\r\n<\/pre>\n<p>The problem came with <code>syslog<\/code> daemon restarts.  Every time the <code>syslog<\/code> was stopped, the EOF was sent to the pipe and the script stopped reading it.<\/p>\n<p><!--more--><\/p>\n<p>First approach was to nest the pipe reading into an endless loop like this:<\/p>\n<pre>\r\nwhile (1) {\r\n  open (SYSLOG, \"<$named_pipe\") \r\n    or die \"Couldn't open $named_pipe: $!\\n\";\r\n\r\n  while (<SYSLOG>) {\r\n    do_processing($_);\r\n  }\r\n\r\n  close(SYSLOG);\r\n  print \"Syslog was restarted\\n\" if ($debug);\r\n}\r\n<\/pre>\n<p>While it worked, it wasn&#8217;t a very nice looking solution.   A couple of alternative ideas that involved signal handling and <code>kill -HUP<\/code> came around but were also disregarded.<\/p>\n<p>A much better looker approach was found in the book by W. Richard Stevens &#8221; UNIX Network Programming, Volume 2, Second Edition: Interprocess Communications&#8221;.  The idea is simpe &#8211; the same script that opens the named pipe for reading, should open the same pipe for writing.  This way, the pipe will stay open as long as the script is working.<\/p>\n<p>The following code works:<\/p>\n<pre>\r\nopen (SYSLOG, \"<$named_pipe\") \r\n  or die \"Couldn't open $named_pipe for reading: $!\\n\";\r\n\r\nopen (SYSLOG_W, \">$named_pipe\") \r\n  or die \"Couldn't open $named_pipe for writing: $!\\n\";\r\n\r\n\r\nwhile (<SYSLOG>) {\r\n  do_processing($_);\r\n}\r\n\r\nclose(SYSLOG);\r\n<\/pre>\n<p>A minor improvement was made after reading <code>perldoc -f open<\/code> which suggested that the same file can be open for both reading and writing with a single <code>open<\/code>.  Here is the changed code:<\/p>\n<pre>\r\nopen (SYSLOG, \"+<$named_pipe\") \r\n  or die \"Couldn't open $named_pipe: $!\\n\";\r\n\r\nwhile (<SYSLOG>) {\r\n  do_processing($_);\r\n}\r\n\r\nclose(SYSLOG);\r\n<\/pre>\n<p>Simple and elegant &#8211; just as code should be.<\/p>\n<p>Drop me a line if you know of any other ways to improve the snippet above.<\/p>\n<!-- google_ad_section_end -->\n","protected":false},"excerpt":{"rendered":"<!-- google_ad_section_start -->\n<p>The collegue of mine came across a problem that developed into an interesting solution that I decided to share with the world. Actually, I think the world is pretty much aware of the solution, but just in case that I will ever be looking for this solution again, I&#8217;ll have it handy here. The task &hellip; <a href=\"https:\/\/mamchenkov.net\/wordpress\/2005\/09\/14\/working-with-named-pipes-in-perl\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Working with named pipes in Perl<\/span><\/a><\/p>\n<!-- google_ad_section_end -->\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_links_to":"","_links_to_target":""},"categories":[1,2294,6,18,62],"tags":[616,1531,36,659,2011,253],"keyring_services":[],"class_list":["post-9451","post","type-post","status-publish","format-standard","hentry","category-general","category-books","category-linux","category-programming","category-technology","tag-filesystem","tag-operating-systems","tag-perl","tag-pipes","tag-richard-stevens","tag-unix"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[{"id":11434,"url":"https:\/\/mamchenkov.net\/wordpress\/2008\/11\/25\/on-remote-logging-with-syslog\/","url_meta":{"origin":9451,"position":0},"title":"On remote logging with syslog","author":"Leonid Mamchenkov","date":"November 25, 2008","format":false,"excerpt":"We've been doing some interesting things at work, as always, with yet more people and Linux boxes.\u00a0 And of the side effects of mixing people, Linux boxes, and several locations is this need for some sort of centralized logging.\u00a0 Luckily we have either syslog-ng or rsyslog daemons installed on each\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10015,"url":"https:\/\/mamchenkov.net\/wordpress\/2006\/03\/27\/the-ultimate-solution-for-problem-solving\/","url_meta":{"origin":9451,"position":1},"title":"The ultimate solution for problem solving","author":"Leonid Mamchenkov","date":"March 27, 2006","format":false,"excerpt":"I believe I have figured it out completely now. Every problem in the Universe has a solution. And not only that, but you can find the solution yourself. If you searched enough, but never figured it out, then here is a little tip for you. Get youself an IRC client.\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":27447,"url":"https:\/\/mamchenkov.net\/wordpress\/2017\/03\/22\/bulletproof-bash-stop-script-on-error\/","url_meta":{"origin":9451,"position":2},"title":"Bulletproof Bash : Stop script on error","author":"Leonid Mamchenkov","date":"March 22, 2017","format":false,"excerpt":"The other day I've been puzzled by the results of a cron job script. \u00a0The bash script in question was written in a hurry a while back, and I was under the assumption that if any of its steps fail, the whole script will fail. \u00a0I was wrong. \u00a0Some commands\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":7792,"url":"https:\/\/mamchenkov.net\/wordpress\/2004\/08\/12\/advantages-of-mod_perl-over-java-servlets\/","url_meta":{"origin":9451,"position":3},"title":"Advantages of mod_perl over Java servlets","author":"Leonid Mamchenkov","date":"August 12, 2004","format":false,"excerpt":"Pretty often, in the mailing lists, people ask why should they use one solution instead of another. There is a similar discussion on mod_perl mailing list. While there are, as usual, a number of reasons to use Perl over Java, it gets interesting when it comes to examples and references.\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":23500,"url":"https:\/\/mamchenkov.net\/wordpress\/2015\/02\/11\/free-alternative-to-splunk-using-fluentd\/","url_meta":{"origin":9451,"position":4},"title":"Free Alternative to Splunk Using Fluentd","author":"Leonid Mamchenkov","date":"February 11, 2015","format":"link","excerpt":"Free Alternative to Splunk Using Fluentd - now, this combination of Elasticsearch, Kibana, and Fluentd looks rather sexy.","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"kibana screenshot","src":"https:\/\/i0.wp.com\/mamchenkov.net\/wordpress\/wp-content\/uploads\/2015\/02\/kibana-screenshot-500x341.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":9196,"url":"https:\/\/mamchenkov.net\/wordpress\/2005\/07\/05\/profiling-perl\/","url_meta":{"origin":9451,"position":5},"title":"Profiling perl","author":"Leonid Mamchenkov","date":"July 5, 2005","format":false,"excerpt":"After I've complained on #perl channel that dprofpp (perl profiler) dies on me, I received a number of suggestions on how to profile the code without using dprofpp. Ideas ranged from elemetary insertion of 'print \"Started\", time,\"\\n\"' to all sorts of dynamic subroutine overloading. Here is one elegant solution that\u2026","rel":"","context":"In &quot;All&quot;","block_context":{"text":"All","link":"https:\/\/mamchenkov.net\/wordpress\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/9451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/comments?post=9451"}],"version-history":[{"count":0,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/posts\/9451\/revisions"}],"wp:attachment":[{"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/media?parent=9451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/categories?post=9451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/tags?post=9451"},{"taxonomy":"keyring_services","embeddable":true,"href":"https:\/\/mamchenkov.net\/wordpress\/wp-json\/wp\/v2\/keyring_services?post=9451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}