Initial add of defaria.com
[clearscm.git] / defaria.com / blogs / Status / 2006 / 11 / perl-style.html
1 <!DOCTYPE html>
2 <html lang="en-us" itemscope itemtype="http://schema.org/Article">
3   <head>
4     <meta charset="utf-8">
5     <meta name="description" content="A little bit about Perl Style By and large I feel that massively indented and thus multi level conditional statements are not handled well by most people. You see as your evaluating the code you have to "put on the...">
6     <meta name="generator" content="Movable Type 5.2.3">
7     <title>Perl Style - Status</title>
8     <link rel="alternate" type="application/atom+xml" title="Recent Entries" href="http://defaria.com/blogs/Status/atom.xml">
9     <link rel="canonical" href="http://defaria.com/blogs/Status/2006/11/perl-style.html">
10     <meta name="viewport" content="width=device-width,initial-scale=1">
11     <link rel="stylesheet" href="http://defaria.com/blogs/Status/styles.css">
12     <!--[if lt IE 9]>
13     <link rel="stylesheet" href="http://defaria.com/blogs/Status/styles_ie.css">
14     <script src="/mt/mt-static/support/theme_static/rainier/js/html5shiv.js"></script>
15     <![endif]-->
16     
17     <link rel="start" href="http://defaria.com/blogs/Status/">
18
19     <link rel="prev" href="http://defaria.com/blogs/Status/2006/11/more-gpdb-fixes.html" title="More GPDB fixes">
20     <link rel="next" href="http://defaria.com/blogs/Status/2006/12/gpdb-bug-fixed.html" title="GPDB Bug Fixed">
21     <!-- Open Graph Protocol -->
22     <meta property="og:type" content="article">
23     <meta property="og:locale" content="en-us">
24     <meta property="og:title" content="Perl Style">
25     <meta property="og:url" content="http://defaria.com/blogs/Status/2006/11/perl-style.html">
26     <meta property="og:description" content="A little bit about Perl Style By and large I feel that massively indented and thus multi level conditional statements are not handled well by most people. You see as your evaluating the code you have to &quot;put on the...">
27     <meta property="og:site_name" content="Status">
28     <meta property="og:image" content="/mt/mt-static/support/theme_static/rainier/img/siteicon-sample.png">
29     <!-- Metadata -->
30     <meta itemprop="description" content="A little bit about Perl Style By and large I feel that massively indented and thus multi level conditional statements are not handled well by most people. You see as your evaluating the code you have to "put on the...">
31     <link itemprop="url" href="http://defaria.com/blogs/Status/2006/11/perl-style.html">
32     <link itemprop="image" href="/mt/mt-static/support/theme_static/rainier/img/siteicon-sample.png">
33     
34   </head>
35   <body>
36     <div id="container">
37       <div id="container-inner">
38         <header id="header" role="banner">
39           <div id="header-inner">
40             <div id="header-content">
41               <h1>
42                 <a href="http://defaria.com/blogs/Status/">
43
44                   Status
45
46                 </a>
47               </h1>
48               
49             </div>
50
51             <nav role="navigation">
52           <ul>
53             <li><a href="http://defaria.com/blogs/Status/">Home</a></li>
54
55
56           </ul>
57         </nav>
58
59           </div>
60         </header>
61         <div id="content">
62           <div id="content-inner">
63             <ul class="breadcrumb breadcrumb-list">
64               <li class="breadcrumb-list-item"><a href="http://defaria.com/blogs/Status/">Home</a></li>
65               <li class="breadcrumb-list-item">Perl Style</li>
66             </ul>
67             <div id="individual-main" class="main" role="main">
68               <article id="entry-1855" class="entry entry-asset asset hentry">
69                 <div class="asset-header">
70                   <h2 itemprop="name" class="asset-name entry-title">Perl Style</h2>
71                   <footer class="asset-meta">
72                     <ul class="asset-meta-list">
73                       <li class="asset-meta-list-item">Posted on <time datetime="2006-11-29T06:38:44-08:00" itemprop="datePublished">November 29, 2006</time></li>
74                       <li class="asset-meta-list-item">by <span class="author entry-author vcard"></span></li>
75
76   
77                       <li class="asset-meta-list-item">in <a itemprop="articleSection" rel="tag" href="http://defaria.com/blogs/Status/texas-instruments/">Texas Instruments</a></li>
78   
79
80                    </ul>
81                 </footer>
82                 </div>
83                 <div class="entry-content asset-content" itemprop="articleBody">
84                   <h2>A little bit about Perl Style</h2>
85
86 <p>By and large I feel that massively indented and thus multi level conditional statements are not handled well by most people. You see as your evaluating the code you have to "put on the mind stack" each condition and bear it in mind as you successively consider each of the nestings of code. As such I always "look for ways out" of the script/function/routine that I'm in. The idea is to avoid nesting by handling error conditions and other "if this happens then we're done" first, often die'ing, erroring out, returning the script/function or subroutine. The net effect is that the rest of the code, the code that normally executes, is shifted to the left a level or two. Additionally complicated, repeated or complex code can be written into a well named subroutine and called from the main or other functions when needed. To me this all makes the code a lot easier to read.</p>
87
88 <p>As an example, and again, I'm not trying to pick on your script specifically and I realize that this might not be the final version, but in reality it contains only a call to GetOptions and 2 if statements. The first if statement merely calls usage if -h is specified. BTW this could be re-written as:</p>
89
90 <div class=code><pre>
91 GetOptions (
92   'f=s',      \$facility,
93   'p=i',      \$port,
94   'a=s',      \$XAID,
95   'w=s',      \$password,
96   'n=s',      \$projName,
97   'g=s',      \$unixGroup,
98   's=s',      \$serverName,
99   'c=s',      \$cachePath,
100   'h',        sub { usage },
101 ) || usage;
102 </pre></div>
103
104 <p>IOW rather than define an $h variable and then set it via GetOptions then test it afterwards only to call usage, define an anonymous subroutine can calls usage directly from GetOptions. Or, since anything other than the stated options displays usage (as per the "|| usage") simply drop the "h" line entirely. If the user specifies -h then it's unknown and usage is displayed. Then $h can be removed as well as the if test following GetOptions.</p>
105
106 <p>The next if statement tests that all of the above variables have been defined. If so then all real processing happens inside that if (and other nested if's therein). Otherwise usage is called. In following with the "looking for a reason to get out" philosophy how about:</p>
107
108 <div class=code><pre>
109 usage unless (
110   defined $password   &amp;&amp;
111   defined $XAID       &amp;&amp;
112   defined $projName   &amp;&amp;
113   defined $serverName &amp;&amp;
114   defined $unixGroup  &amp;&amp;
115   defined $cachePath  &amp;&amp;
116   defined $port       &amp;&amp;
117   defined $facility
118 );
119 </pre></div>
120
121 <p>Basically this says, "if any of these are not defined call usage" and since usage doesn't return everything inside the old if statement can be shifted to the left. Other opportunities exist to continue to apply this philosophy of "getting out while you can" and reducing the nesting level of the program. For example, inside the old if statement you
122 check to see if the login of GPDB successfully made you an administrator. If not you're gonna stop the script right there. So then how about something like:</p>
123
124 <div class=code><pre>
125 my $login = gpdb_login ($XAID, $password);
126
127 error "$XAID is not an administrator", 1 if $login !~ /administrator/;
128 </pre></div>
129
130 <p>gpdb_login returns a string that will contain "administrator" if you were able to login as an administrator. Here I use error, a routine that I often use (in fact I'd like to publish it to TI as a common utility) that writes an error message to STDERR and optionally exits if
131 the second parameter is not 0.</p>
132                   
133                 </div>
134                 <nav class="page-navigation entry-navigation pagination content-nav">
135                   <ul class="page-navigation-list">
136
137                     <li class="page-navigation-list-item page-navigation-prev"><a rel="prev" href="http://defaria.com/blogs/Status/2006/11/more-gpdb-fixes.html" title="More GPDB fixes">Previous entry</a></li>
138
139
140                     <li class="page-navigation-list-item page-navigation-next"><a rel="next" href="http://defaria.com/blogs/Status/2006/12/gpdb-bug-fixed.html" title="GPDB Bug Fixed">Next entry</a></li>
141
142                   </ul>
143                 </nav>
144                 <!--
145 <aside id="zenback" class="zenback feedback">
146   Please paste Zenback script code here.
147 </aside>
148 -->
149                 
150                 
151               </article>
152             </div>
153             <aside class="widgets related" role="complementary">
154               <nav class="widget-search widget">
155   <div class="widget-content">
156     <form method="get" id="search" action="http://defaria.com/mt/mt-search.cgi">
157       <div>
158         <input type="text" name="search" value="" placeholder="Search...">
159
160         <input type="hidden" name="IncludeBlogs" value="8">
161
162         <input type="hidden" name="limit" value="20">
163         <button type="submit" name="button">
164           <img alt="Search" src="/mt/mt-static/support/theme_static/rainier/img/search-icon.png">
165         </button>
166       </div>
167     </form>
168   </div>
169 </nav>
170 <nav class="widget-archive-category widget">
171   <h3 class="widget-header">Categories</h3>
172   <div class="widget-content">
173     
174       
175     <ul class="widget-list">
176       
177       
178       <li class="widget-list-item"><a href="http://defaria.com/blogs/Status/ameriquest/">Ameriquest (99)</a>
179       
180       
181       </li>
182       
183     
184       
185       
186       <li class="widget-list-item"><a href="http://defaria.com/blogs/Status/audience/">Audience (3)</a>
187       
188       
189       </li>
190       
191     
192       
193       
194       <li class="widget-list-item"><a href="http://defaria.com/blogs/Status/broadcom/">Broadcom (76)</a>
195       
196       
197       </li>
198       
199     
200       
201       
202       <li class="widget-list-item"><a href="http://defaria.com/blogs/Status/gpdb/">GPDB (35)</a>
203       
204       
205       </li>
206       
207     
208       
209       
210       <li class="widget-list-item"><a href="http://defaria.com/blogs/Status/general-dynamics/">General Dynamics (61)</a>
211       
212       
213       </li>
214       
215     
216       
217       
218       <li class="widget-list-item"><a href="http://defaria.com/blogs/Status/general-electric/">General Electric (13)</a>
219       
220       
221       </li>
222       
223     
224       
225       
226       <li class="widget-list-item"><a href="http://defaria.com/blogs/Status/hewlett-packard/">Hewlett Packard (13)</a>
227       
228       
229       </li>
230       
231     
232       
233       
234       <li class="widget-list-item"><a href="http://defaria.com/blogs/Status/lynuxworks/">LynuxWorks (162)</a>
235       
236       
237       </li>
238       
239     
240       
241       
242       <li class="widget-list-item"><a href="http://defaria.com/blogs/Status/pqa/">PQA (35)</a>
243       
244       
245       </li>
246       
247     
248       
249       
250       <li class="widget-list-item"><a href="http://defaria.com/blogs/Status/salira/">Salira (79)</a>
251       
252       
253       </li>
254       
255     
256       
257       
258       <li class="widget-list-item"><a href="http://defaria.com/blogs/Status/tellabs/">Tellabs (2)</a>
259       
260       
261       </li>
262       
263     
264       
265       
266       <li class="widget-list-item"><a href="http://defaria.com/blogs/Status/texas-instruments/">Texas Instruments (31)</a>
267       
268       
269       </li>
270       
271     </ul>
272       
273     
274   </div>
275 </nav>
276   
277
278 <nav class="widget-archive-dropdown widget">
279   <h3 class="widget-header">Archives</h3>
280   <div class="widget-content">
281     <select>
282       <option>Select a Month...</option>
283     
284       <option value="http://defaria.com/blogs/Status/2016/02/">February 2016</option>
285     
286   
287     
288       <option value="http://defaria.com/blogs/Status/2014/09/">September 2014</option>
289     
290   
291     
292       <option value="http://defaria.com/blogs/Status/2014/04/">April 2014</option>
293     
294   
295     
296       <option value="http://defaria.com/blogs/Status/2014/03/">March 2014</option>
297     
298   
299     
300       <option value="http://defaria.com/blogs/Status/2013/02/">February 2013</option>
301     
302   
303     
304       <option value="http://defaria.com/blogs/Status/2012/09/">September 2012</option>
305     
306   
307     
308       <option value="http://defaria.com/blogs/Status/2012/08/">August 2012</option>
309     
310   
311     
312       <option value="http://defaria.com/blogs/Status/2012/05/">May 2012</option>
313     
314   
315     
316       <option value="http://defaria.com/blogs/Status/2012/04/">April 2012</option>
317     
318   
319     
320       <option value="http://defaria.com/blogs/Status/2012/02/">February 2012</option>
321     
322   
323     
324       <option value="http://defaria.com/blogs/Status/2012/01/">January 2012</option>
325     
326   
327     
328       <option value="http://defaria.com/blogs/Status/2011/10/">October 2011</option>
329     
330   
331     
332       <option value="http://defaria.com/blogs/Status/2011/07/">July 2011</option>
333     
334   
335     
336       <option value="http://defaria.com/blogs/Status/2010/09/">September 2010</option>
337     
338   
339     
340       <option value="http://defaria.com/blogs/Status/2010/08/">August 2010</option>
341     
342   
343     
344       <option value="http://defaria.com/blogs/Status/2010/04/">April 2010</option>
345     
346   
347     
348       <option value="http://defaria.com/blogs/Status/2010/03/">March 2010</option>
349     
350   
351     
352       <option value="http://defaria.com/blogs/Status/2010/02/">February 2010</option>
353     
354   
355     
356       <option value="http://defaria.com/blogs/Status/2009/05/">May 2009</option>
357     
358   
359     
360       <option value="http://defaria.com/blogs/Status/2009/04/">April 2009</option>
361     
362   
363     
364       <option value="http://defaria.com/blogs/Status/2008/07/">July 2008</option>
365     
366   
367     
368       <option value="http://defaria.com/blogs/Status/2008/05/">May 2008</option>
369     
370   
371     
372       <option value="http://defaria.com/blogs/Status/2008/04/">April 2008</option>
373     
374   
375     
376       <option value="http://defaria.com/blogs/Status/2008/03/">March 2008</option>
377     
378   
379     
380       <option value="http://defaria.com/blogs/Status/2008/02/">February 2008</option>
381     
382   
383     
384       <option value="http://defaria.com/blogs/Status/2008/01/">January 2008</option>
385     
386   
387     
388       <option value="http://defaria.com/blogs/Status/2007/12/">December 2007</option>
389     
390   
391     
392       <option value="http://defaria.com/blogs/Status/2007/11/">November 2007</option>
393     
394   
395     
396       <option value="http://defaria.com/blogs/Status/2007/10/">October 2007</option>
397     
398   
399     
400       <option value="http://defaria.com/blogs/Status/2007/09/">September 2007</option>
401     
402   
403     
404       <option value="http://defaria.com/blogs/Status/2007/08/">August 2007</option>
405     
406   
407     
408       <option value="http://defaria.com/blogs/Status/2007/07/">July 2007</option>
409     
410   
411     
412       <option value="http://defaria.com/blogs/Status/2007/06/">June 2007</option>
413     
414   
415     
416       <option value="http://defaria.com/blogs/Status/2007/05/">May 2007</option>
417     
418   
419     
420       <option value="http://defaria.com/blogs/Status/2007/04/">April 2007</option>
421     
422   
423     
424       <option value="http://defaria.com/blogs/Status/2007/03/">March 2007</option>
425     
426   
427     
428       <option value="http://defaria.com/blogs/Status/2007/01/">January 2007</option>
429     
430   
431     
432       <option value="http://defaria.com/blogs/Status/2006/12/">December 2006</option>
433     
434   
435     
436       <option value="http://defaria.com/blogs/Status/2006/11/">November 2006</option>
437     
438   
439     
440       <option value="http://defaria.com/blogs/Status/2006/10/">October 2006</option>
441     
442   
443     
444       <option value="http://defaria.com/blogs/Status/2006/09/">September 2006</option>
445     
446   
447     
448       <option value="http://defaria.com/blogs/Status/2006/07/">July 2006</option>
449     
450   
451     
452       <option value="http://defaria.com/blogs/Status/2006/06/">June 2006</option>
453     
454   
455     
456       <option value="http://defaria.com/blogs/Status/2006/05/">May 2006</option>
457     
458   
459     
460       <option value="http://defaria.com/blogs/Status/2006/04/">April 2006</option>
461     
462   
463     
464       <option value="http://defaria.com/blogs/Status/2006/03/">March 2006</option>
465     
466   
467     
468       <option value="http://defaria.com/blogs/Status/2006/02/">February 2006</option>
469     
470   
471     
472       <option value="http://defaria.com/blogs/Status/2006/01/">January 2006</option>
473     
474   
475     
476       <option value="http://defaria.com/blogs/Status/2005/12/">December 2005</option>
477     
478   
479     
480       <option value="http://defaria.com/blogs/Status/2005/11/">November 2005</option>
481     
482   
483     
484       <option value="http://defaria.com/blogs/Status/2005/10/">October 2005</option>
485     
486   
487     
488       <option value="http://defaria.com/blogs/Status/2005/09/">September 2005</option>
489     
490   
491     
492       <option value="http://defaria.com/blogs/Status/2005/08/">August 2005</option>
493     
494   
495     
496       <option value="http://defaria.com/blogs/Status/2005/07/">July 2005</option>
497     
498   
499     
500       <option value="http://defaria.com/blogs/Status/2005/06/">June 2005</option>
501     
502   
503     
504       <option value="http://defaria.com/blogs/Status/2005/05/">May 2005</option>
505     
506   
507     
508       <option value="http://defaria.com/blogs/Status/2005/04/">April 2005</option>
509     
510   
511     
512       <option value="http://defaria.com/blogs/Status/2005/03/">March 2005</option>
513     
514   
515     
516       <option value="http://defaria.com/blogs/Status/2005/02/">February 2005</option>
517     
518   
519     
520       <option value="http://defaria.com/blogs/Status/2005/01/">January 2005</option>
521     
522   
523     
524       <option value="http://defaria.com/blogs/Status/2004/12/">December 2004</option>
525     
526   
527     
528       <option value="http://defaria.com/blogs/Status/2004/09/">September 2004</option>
529     
530   
531     
532       <option value="http://defaria.com/blogs/Status/2004/08/">August 2004</option>
533     
534   
535     
536       <option value="http://defaria.com/blogs/Status/2004/07/">July 2004</option>
537     
538   
539     
540       <option value="http://defaria.com/blogs/Status/2004/06/">June 2004</option>
541     
542   
543     
544       <option value="http://defaria.com/blogs/Status/2004/05/">May 2004</option>
545     
546   
547     
548       <option value="http://defaria.com/blogs/Status/2004/04/">April 2004</option>
549     
550   
551     
552       <option value="http://defaria.com/blogs/Status/2004/03/">March 2004</option>
553     
554   
555     
556       <option value="http://defaria.com/blogs/Status/2004/02/">February 2004</option>
557     
558   
559     
560       <option value="http://defaria.com/blogs/Status/2004/01/">January 2004</option>
561     
562   
563     
564       <option value="http://defaria.com/blogs/Status/2003/12/">December 2003</option>
565     
566   
567     
568       <option value="http://defaria.com/blogs/Status/2003/11/">November 2003</option>
569     
570     </select>
571   </div>
572 </nav>
573     
574   
575
576 <div class="widget-syndication widget section">
577   <div class="widget-content">
578     <p><img src="http://defaria.com/mt/mt-static/images/status_icons/feed.gif" alt="Subscribe to feed" width="9" height="9" /> <a href="http://defaria.com/blogs/Status/atom.xml">Subscribe to this blog's feed</a></p>
579
580   </div>
581 </div>
582
583             </aside>
584           </div>
585         </div>
586         <footer id="footer" role="contentinfo">
587           <div id="footer-inner">
588             <div id="footer-content">
589   <nav role="navigation">
590           <ul>
591             <li><a href="http://defaria.com/blogs/Status/">Home</a></li>
592
593
594           </ul>
595         </nav>
596
597   <p class="license">&copy; Copyright 2016.</p>
598   <p class="poweredby">Powered by <a href="http://www.movabletype.org/">Movable Type</a></p>
599 </div>
600           </div>
601         </footer>
602       </div>
603     </div>
604     <script src="http://defaria.com/mt/mt-static/jquery/jquery.min.js"></script>
605     <script src="http://defaria.com/blogs/Status/mt-theme-scale2.js"></script>
606   </body>
607 </html>