|
|||||
|
|
#1 |
|
|
I just finished a script for a client and will be working on the do***entation shortly. The script uses a fairly complex data structure that looks like: ################################################## ################### # Redesigned data structure. ################################################## ################### # %$hash: # ${$hash}{Datalist} = \%datalist: # ${${$hash}{Datalist}}{$dl} = \% # ${${${$hash}{Datalist}}{$dl}}{host} = $host # ${${${$hash}{Datalist}}{$dl}}{sid} = $sid # ${${${$hash}{Datalist}}{$dl}}{suffix} = $suffix # ${${${$hash}{Datalist}}{$dl}}{stream} = $stream #-------------------------------------------------------------------- # ${$hash}{Host} = \%host_list; # ${${$hash}{Host}}{$host} = [ \@dls, \%sids ] # ${${${$hash}{Host}}{$host}}[0] = \@dls # ${${${$hash}{Host}}{$host}}[1] = \%sids # ${${${${$hash}{Host}}{$host}}[1]}{$sid} = [ @dls, %lvs ] ################################################## ################### Does anyone have any tips on how I could do***ent that clearly - for people that aren't used to complex data structures in perl? Any formatting/other hints/tips greatly appreciated. Doug -- -------- Senior UNIX Admin O'Leary Computer Enterprises dkoleary@olearycomputers.com (w) 630-904-6098 (c) 630-248-2749 resume: http://home.comcast.net/~dkoleary/resume.html |
|
|
#2 |
|
|
DO> Hey all; DO> I just finished a script for a client and will be working on the DO> do***entation shortly. The script uses a fairly complex data DO> structure that looks like: DO> ################################################## ################### DO> # Redesigned data structure. DO> ################################################## ################### DO> # %$hash: DO> # ${$hash}{Datalist} = \%datalist: DO> # ${${$hash}{Datalist}}{$dl} = \% DO> # ${${${$hash}{Datalist}}{$dl}}{host} = $host DO> # ${${${$hash}{Datalist}}{$dl}}{sid} = $sid DO> # ${${${$hash}{Datalist}}{$dl}}{suffix} = $suffix DO> # ${${${$hash}{Datalist}}{$dl}}{stream} = $stream DO> #-------------------------------------------------------------------- DO> # ${$hash}{Host} = \%host_list; DO> # ${${$hash}{Host}}{$host} = [ \@dls, \%sids ] DO> # ${${${$hash}{Host}}{$host}}[0] = \@dls DO> # ${${${$hash}{Host}}{$host}}[1] = \%sids DO> # ${${${${$hash}{Host}}{$host}}[1]}{$sid} = [ @dls, %lvs ] DO> ################################################## ################### DO> Does anyone have any tips on how I could do***ent that DO> clearly - for people that aren't used to complex data DO> structures in perl? GACK!! don't use that deref style in the docs or the code. that is impossible to read. use something like this (untested): $hash->{Host}{$host}[0] = 'foo' ; note how much more readable that is for both coding and do***entation purposes? no one uses the other deref style for anything more than 1 deep as it is impossible to parse out visually. DO> Any formatting/other hints/tips greatly appreciated. use Data: umper to dump out example trees.uri -- Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com --Perl Consulting, Stem Development, Systems Architecture, Design and Coding- Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org |
|
|
#3 |
|
|
>>>>>> "DO" == Doug O'Leary <dkoleary@olearycomputers.com> writes: > DO> # ${${${$hash}{Datalist}}{$dl}}{stream} = $stream > GACK!! > DO> Any formatting/other hints/tips greatly appreciated. > > use Data: umper to dump out example trees.Rewrite it, to use OO programming. Also, do***entation should be your first step in design, not last. I think with 3 or 4 cl***es you can make it way more readable and less error prone. -- John Small Perl scripts: http://johnbokma.com/perl/ Perl programmer available: http://castleamber.com/ Happy Customers: http://castleamber.com/testimonials.html |
|
|
#4 |
|
|
On 2004-11-18, John Bokma <postmaster@castleamber.com> wrote:
> > Rewrite it, to use OO programming. > > Also, do***entation should be your first step in design, not last. > > I think with 3 or 4 cl***es you can make it way more readable and less > error prone. Wow; I can only aspire to being good enough to critique code without seeing it... That's truly impressive. I will be certain to give your thoughts all the consideration they're due. Doug -- -------- Senior UNIX Admin O'Leary Computer Enterprises dkoleary@olearycomputers.com (w) 630-904-6098 (c) 630-248-2749 resume: http://home.comcast.net/~dkoleary/resume.html |
|
|
#5 |
|
|
On 2004-11-18, Uri Guttman <uri@stemsystems.com> wrote:
> > GACK!! > > don't use that deref style in the docs or the code. that is impossible > to read. use something like this (untested): > > $hash->{Host}{$host}[0] = 'foo' ; > > note how much more readable that is for both coding and do***entation > purposes? no one uses the other deref style for anything more than 1 > deep as it is impossible to parse out visually. > I've gotten used to it after all this time. I'll try that way though. Thanks. > DO> Any formatting/other hints/tips greatly appreciated. > > use Data: umper to dump out example trees.I'll look into it; thanks again. Doug -- -------- Senior UNIX Admin O'Leary Computer Enterprises dkoleary@olearycomputers.com (w) 630-904-6098 (c) 630-248-2749 resume: http://home.comcast.net/~dkoleary/resume.html |
|
|
#6 |
|
|
>>>>> "DO" == Doug O'Leary <dkoleary@olearycomputers.com> writes:
DO> On 2004-11-18, Uri Guttman <uri@stemsystems.com> wrote: >> >> GACK!! >> >> don't use that deref style in the docs or the code. that is impossible >> to read. use something like this (untested): >> >> $hash->{Host}{$host}[0] = 'foo' ; >> >> note how much more readable that is for both coding and do***entation >> purposes? no one uses the other deref style for anything more than 1 >> deep as it is impossible to parse out visually. >> DO> I've gotten used to it after all this time. I'll try that way DO> though. Thanks. but no one else uses that style. think about it. code for yourself or for others - which is more professional? DO> Any formatting/other hints/tips greatly appreciated. >> >> use Data: umper to dump out example trees.DO> I'll look into it; thanks again. anyone who does perl data structure stuff without using data::dumper as a debugging tool is not doing it right. (YAML can be used too but it isn't standard in the perl core). uri -- Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com --Perl Consulting, Stem Development, Systems Architecture, Design and Coding- Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org |
|
|
#7 |
|
|
Doug O'Leary <dkoleary@olearycomputers.com> wrote in
news:slrncpq11c.ci7.dkoleary@x1-6-00-b0-d0-c1-44-09.comcast.net: > On 2004-11-18, John Bokma <postmaster@castleamber.com> wrote: >> >> Rewrite it, to use OO programming. >> >> Also, do***entation should be your first step in design, not last. >> >> I think with 3 or 4 cl***es you can make it way more readable and less >> error prone. > > Wow; I can only aspire to being good enough to critique code without > seeing it... That's truly impressive. I will be certain to give your > thoughts all the consideration they're due. I am not sure if you intended this to be a sarcastic remark but it does come accross that way. There is no way you came up with the data structure you came up with through "good" design. Take John's recommendation to heart, figure out the relevant abstractions. Sinan. |
|
|
#8 |
|
|
On Thu, 18 Nov 2004 20:24:23 GMT, Doug O'Leary
<dkoleary@olearycomputers.com> wrote: >I've gotten used to it after all this time. But surely the point that is being made in comments about this is that *other* people aren't used to it, and by definition they are the ones that are the target for your do***entation? -- Henry Law <>< Manchester, England |
|
|
#9 |
|
|
On 2004-11-18, Uri Guttman <uri@stemsystems.com> wrote:
> > but no one else uses that style. think about it. code for yourself or > for others - which is more professional? Actually, I usually code for other UNIX administrators. This script, for instance, talks to oracle databases, obtains a list of logical volumes its using, then verifies/updates Omniback datalists to ensure the databases are getting backed up correctly. Another script, more complex code, but less complex data structure, hits up to 1800 routers/switches via snmp every 5 minutes and runs a user specified command if a user specified formula evaulates to true. ### Config file snip IP = 1.1.1.1 Node = rtr-01 Comm = not_real_string Formula = ((delta_Outqdrop.1) > CSCOoutbw.1 * .03) \ || ((delta_Outqdrop.2) > CSCOoutbw.2 * .03) \ || ((delta_Outqdrop.4) > CSCOoutbw.4 * .03) Command = `$opcmsg node=$Node app=SNMP obj=SNMP msg_grp=SNMP severity=warning msg_text="Output queue drops > 3% bandwidth."` ### That client is still using that script - at least they were two months ago when I talked to them last... > anyone who does perl data structure stuff without using data::dumper as > a debugging tool is not doing it right. (YAML can be used too but it > isn't standard in the perl core). I'm not sure I buy that 100%. After figuring out what was needed, I was able to generate this data structure w/o errors first time running. It read the config fiie, created the structure, then read it back out again using simple loops and print statements.. ################################################## #################### #### Code to read hashes from hash_ref ################################################## #################### # foreach my $dl (sort keys %{${$hash}{'Datalist'}}) # { print "$dl\n\t"; # printf ("\t%-8s %-7s %-4s %d\n", # ${${${$hash}{'Datalist'}}{$dl}}{'Host'}, # ${${${$hash}{'Datalist'}}{$dl}}{'Sid'}, # ${${${$hash}{'Datalist'}}{$dl}}{'Suffix'} || "N/A", # ${${${$hash}{'Datalist'}}{$dl}}{'Stream'} || 1); # } # print "-" x 35 . "\n"; # foreach my $host (sort keys %{${$hash}{'Host'}}) # { print "$host\n\tDatalists\n"; # my $array_ref = \@{${${${$hash}{'Host'}}{$host}}[0]}; # foreach my $dl (sort @{$array_ref}) # { print "\t\t$dl\n"; } # print "\tSids\n"; # my $hash_ref = \%{${${${$hash}{'Host'}}{$host}}[1]}; # foreach my $sid (sort keys %{$hash_ref}) # { print "\t\t$sid\n"; # foreach my $dl (sort @{${${$hash_ref}{$sid}}[0]}) # { print "\t\t\t$dl\n"; } # } # } # print "-" x 35 . "\n"; data::dumper may do all that work for me; dunno. I'll look into it though. So far, though, I haven't *needed* it - and all my scripts work the way they're supposed to.. Thanks again for the input, though. I like learning new ways to do things. Doug -- -------- Senior UNIX Admin O'Leary Computer Enterprises dkoleary@olearycomputers.com (w) 630-904-6098 (c) 630-248-2749 resume: http://home.comcast.net/~dkoleary/resume.html |
|
|
#10 |
|
|
On 2004-11-18, A. Sinan Unur <1usa@llenroc.ude.invalid> wrote:
> > I am not sure if you intended this to be a sarcastic remark but it does > come accross that way. > > There is no way you came up with the data structure you came up with > through "good" design. Take John's recommendation to heart, figure out the > relevant abstractions. To be blatantly clear - Yes, it was meant to be sarcastic. It was meant that way and meant to be taken that way. He has no logical basis for critiquing code he hasn't seen. His attempt to do so, to me, proves that he jumps to conclusions without analyzing facts. Therefore, his opinions on this subject are ignored - and any further opinions that I run into will be strenuously verified before being considered. Much like you have no basis in logic for concluding that I developed this data structure through poor design. You have no idea what process was followed or not followed. The only thing you have is a complex datastructure implemented in a manner that, although not common, is, at least according to the perl books, perfectly acceptable. Proof, they say, is in the fact that the code works. Therefore, your opinions, like John's, are now suspect. As I've mentioned to Uri, I will examine rewriting the code to use the C-style pointer syntax. Who know, I may like it better. I may not, but still force myself to use it in order to make more easily supported code. But, no; I will not take John's recommendation to heart. Nor, will I listen to pedantic pinheads who critique code or procedures they haven't seen. Have a nice day. Doug -- -------- Senior UNIX Admin O'Leary Computer Enterprises dkoleary@olearycomputers.com (w) 630-904-6098 (c) 630-248-2749 resume: http://home.comcast.net/~dkoleary/resume.html |