{"id":152,"date":"2004-03-16T06:10:00","date_gmt":"2004-03-16T06:10:00","guid":{"rendered":"http:\/\/jclark.org\/weblog\/Programming\/Perl\/senseless.html"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-30T04:00:00","slug":"senseless","status":"publish","type":"post","link":"https:\/\/jclark.org\/weblog\/2004\/03\/16\/senseless\/","title":{"rendered":"Senseless Acts of Perl"},"content":{"rendered":"<p>At work I spend alot of time working on one of our Solaris dev servers via xterm.  Via many xterms simultaneously, most of the time.  Since I run a local X client on my PC under cygwin, I have a shell script that I run locally that connects to the dev box and launches three xterms in pre-determined screen locations, setting DISPLAY along the way.  <\/p>\n<p>Over the course of a busy morning, this number can grow.  Since I&#8217;m still on a Windows PC, however, I do tend to use my task bar to find windows.  Having six or more taskbar buttons that all say &#8220;xterm&#8221; isn&#8217;t very helpful.  For a while I tried setting my titles to reflect what I&#8217;m doing in each xterm, but this futile.  Partially because I often create, destroy, or repurpose xterms on a whim; but largely because I&#8217;m lazy.  <\/p>\n<p>A while ago, I updated my launch script to label my initial three windows Alpha, Beta, and Gamma.  While the names aren&#8217;t very descriptive, it does differentiate the windows, and I can usually remember what each window is being used for.  When I start launching additional xterms, things can get confusing; I try to remember to add a <code>-title<\/code> and pick a Greek letter not in use, but I did mention I&#8217;m lazy, right?  So today, I decided to do something about it.<\/p>\n<p>The result is <code>addterm<\/code>, one of the more senseless perl scripts I&#8217;ve ever bothered with.  When run, it creates a new xterm with the title set to the name of the first greek letter not currently in use.  If all 24 greek letters are in use, and error message is printed and no xterm is launched.  This is a feature, not a bug.  Close some windows!  The version below is my OS X port. :<\/p>\n<pre><code>#!\/usr\/bin\/perl -w\n\nmy $user = `whoami`;\nmy @ps = split(&quot;\\n&quot;, `ps -o command -U $user`);\nmy @alpha = qw\/Alpha Beta Gamma Delta Epsilon\n               Zeta Eta Theta Iota Kappa Lambda\n               Mu Nu Xi Omicron Pi Rho Sigma\n               Tau Upsilon Phi Chi Psi Omega\/;\nmy $k=0;\nmy %greek = map {$_=&gt;$k++} @alpha;\n\nfor(@ps) {\n    my ($title) = \/^xterm\\s+-title\\s+([^\\s]+)\/ or next;\n    $alpha[$greek{$title}]=) {\n        $next = $_;\n        last;\n    }\n}\n\nif (defined $next) {\n    open STDERR, &#039;&gt;\/dev\/null&#039;; #discard xterm&#039;s whining\n    system(&quot;xterm -title $next &amp; &quot;);\n} else {\n    print STDERR &quot;ERROR: No greek letters free!\\n&quot;;\n}<\/code><\/pre>\n<p>This required a port from the original Solaris version because the script uses <code>ps<\/code> to look for running xterms.  The Solaris version uses <code>ps -o args -u $user<\/code>.  The command should list (only) the full command + args for every process for the username $user.  If you want to use this on another *nix, just test your <code>ps<\/code> command first and adjust accordingly.  You could also change the Greek letters to another finite set, just remember to update the error message.  <\/p>\n<p>Of dubious interest is that fact that I used an array to keep the letters in order and a hash to allow quick indexing into the array.  I dislike having to store the letters twice, but this seemed the best solution.  I have a vague sense that some kind of tied vars may do this more elegantly, but my perl-fu isn&#8217;t quite that strong without cracking the <a href=\"http:\/\/www.amazon.com\/exec\/obidos\/ASIN\/0596000278\/ref%3Dnosim\/jclarkorg-20\/\">Camel<\/a>; did I mention I&#8217;m lazy?  Perhaps tommorow. Improvements welcomed.<\/p>","protected":false},"excerpt":{"rendered":"<p>At work I spend alot of time working on one of our Solaris dev servers via xterm. Via many xterms simultaneously, most of the time. Since I run a local X client on my PC under cygwin, I have a shell script that I run locally that connects to the dev box and launches three [&hellip;]<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-152","post","type-post","status-publish","format-standard","hentry","category-perl"],"_links":{"self":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/posts\/152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/comments?post=152"}],"version-history":[{"count":0,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/posts\/152\/revisions"}],"wp:attachment":[{"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/media?parent=152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/categories?post=152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jclark.org\/weblog\/wp-json\/wp\/v2\/tags?post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}