Hello roboticus,

I know next-to-nothing about electronics, so I can’t comment on the substance of your code. But...

Comments about my coding style are always welcome

...well, since you ask ;-), I think your OO design could be improved.

(1) Statements like delete $g->{nodes}{out_neg}; violate encapsulation. So does using a return bless ..., "Gr"; statement in a non-Gr function such as build_impedence.

(2) The 4 bless ... "Gr" statements effectively create 4 separate constructors, with different names, which is at best confusing. But the real problem here is the superfluous creation of new objects. There is usually no need to return an object from a method call. Instead of:

$atten = build_pad(10); $atten->dump("PAD 10dB"); my $atten2 = build_pad(20); $atten2->dump("PAD 20dB"); $term = build_impedance(50); $term->dump("TERM 50ohm"); $g = $atten2->attach($term, [qw( out_pos in_pos )], [qw( out_neg in_ne +g )]); delete $g->{nodes}{out_neg}; delete $g->{nodes}{out_pos}; $g->dump("PAD 20dB + TERM 50ohm"); $h = $atten->attach($atten2, [qw( out_pos in_pos )], [qw( out_neg in_n +eg )]); delete $h->{nodes}{out_neg}; delete $h->{nodes}{out_pos}; $h->dump("PAD 10dB + PAD 20dB + TERM 50ohm"); my $i = $h->simplify(); $i->dump("RESULT!");

a cleaner interface would allow:

$atten = Gr->new(pad => 10); $atten->dump("PAD 10dB"); my $atten2 = Gr->new(pad => 20); $atten2->dump("PAD 20dB"); $term = Gr->new(impedance => 50); $term->dump("TERM 50ohm"); $atten2->attach($term, [qw( out_pos in_pos )], [qw( out_neg in_neg )]) +; $atten2->delete('nodes', 'out_neg'); $atten2->delete('nodes', 'out_pos'); $atten2->dump("PAD 20dB + TERM 50ohm"); $atten->attach($atten2, [qw( out_pos in_pos )], [qw( out_neg in_neg )] +); $atten->delete('nodes', 'out_neg'); $atten->delete('nodes', 'out_pos'); $atten->dump("PAD 10dB + PAD 20dB + TERM 50ohm"); $atten->simplify(); $atten->dump("RESULT!");

with all the gory details of object manipulation — including cloning, where required — confined to the Gr class.


BTW, this line of code:

my ($in_neg, $in_pos, $out_neg, $out_pos) = (node(), node(), node(), n +ode());

struck me as a challenge: is it possible to remove the duplicate calls? The obvious ... = (node()) x 4 won’t work, because the node() function has to be called four times, not just once. With a little trial-and-error, I found (to my surprise) that this does work (tested on Strawberry Perls 5.20.0 and 5.28.0):

$$_ = node() for \my ($in_neg, $in_pos, $out_neg, $out_pos);

Hope that’s of interest,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Resistor network simplifier by Athanasius
in thread Resistor network simplifier by roboticus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.