Initial add of defaria.com
[clearscm.git] / defaria.com / blogs / Status / archives / 000599.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" id="sixapart-standard">
4 <head>
5    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6    <meta name="generator" content="Movable Type 5.2.3" />
7
8    <link rel="stylesheet" href="http://defaria.com/blogs/Status/styles-site.css" type="text/css" />
9    <link rel="alternate" type="application/atom+xml" title="Atom" href="http://defaria.com/blogs/Status/atom.xml" />
10    <link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="http://defaria.com/blogs/Status/index.xml" />
11
12    <title>Status for Andrew DeFaria: Perl Style</title>
13
14    <link rel="start" href="http://defaria.com/blogs/Status/" title="Home" />
15    <link rel="prev" href="http://defaria.com/blogs/Status/archives/000598.html" title="More GPDB fixes" />
16    <link rel="next" href="http://defaria.com/blogs/Status/archives/000600.html" title="GPDB Bug Fixed" />
17
18    
19
20    
21
22    <script type="text/javascript" src="http://defaria.com/blogs/Status/mt-site.js"></script>
23 </head>
24 <body class="layout-one-column" onload="individualArchivesOnLoad(commenter_name)">
25    <div id="container">
26       <div id="container-inner" class="pkg">
27
28          <div id="banner">
29             <div id="banner-inner" class="pkg">
30                <h1 id="banner-header"><a href="http://defaria.com/blogs/Status/" accesskey="1">Status for Andrew DeFaria</a></h1>
31                <h2 id="banner-description">Searchable status reports and work log</h2>
32             </div>
33          </div>
34
35          <div id="pagebody">
36             <div id="pagebody-inner" class="pkg">
37                <div id="alpha">
38                   <div id="alpha-inner" class="pkg">
39
40                      <p class="content-nav">
41                         <a href="http://defaria.com/blogs/Status/archives/000598.html">&laquo; More GPDB fixes</a> |
42                         <a href="http://defaria.com/blogs/Status/">Main</a>
43                         | <a href="http://defaria.com/blogs/Status/archives/000600.html">GPDB Bug Fixed &raquo;</a>
44                      </p>
45
46                      <a id="a000599"></a>
47                      <div class="entry" id="entry-599">
48                         <h3 class="entry-header">Perl Style</h3>
49                         <div class="entry-content">
50                            <div class="entry-body">
51                               <h2>A little bit about Perl Style</h2>
52
53 <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>
54
55 <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>
56
57 <div class=code><pre>
58 GetOptions (
59   'f=s',      \$facility,
60   'p=i',      \$port,
61   'a=s',      \$XAID,
62   'w=s',      \$password,
63   'n=s',      \$projName,
64   'g=s',      \$unixGroup,
65   's=s',      \$serverName,
66   'c=s',      \$cachePath,
67   'h',        sub { usage },
68 ) || usage;
69 </pre></div>
70
71 <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>
72
73 <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>
74
75 <div class=code><pre>
76 usage unless (
77   defined $password   &amp;&amp;
78   defined $XAID       &amp;&amp;
79   defined $projName   &amp;&amp;
80   defined $serverName &amp;&amp;
81   defined $unixGroup  &amp;&amp;
82   defined $cachePath  &amp;&amp;
83   defined $port       &amp;&amp;
84   defined $facility
85 );
86 </pre></div>
87
88 <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
89 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>
90
91 <div class=code><pre>
92 my $login = gpdb_login ($XAID, $password);
93
94 error "$XAID is not an administrator", 1 if $login !~ /administrator/;
95 </pre></div>
96
97 <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
98 the second parameter is not 0.</p>
99                            </div>
100                            <div id="more" class="entry-more">
101                               
102                            </div>
103                         </div>
104                         <p class="entry-footer">
105                            <span class="post-footers">Posted by  on November 29, 2006  6:38 AM</span> <span class="separator">|</span> <a class="permalink" href="http://defaria.com/blogs/Status/archives/000599.html">Permalink</a>
106                         </p>
107                      </div>
108
109                      
110
111                      
112                   </div>
113                </div>
114             </div>
115          </div>
116       </div>
117    </div>
118 </body>
119 </html>