Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

To me the secret to DRY (which is Do not Repeat Yourself -- I had to look that up) is to convert functionality to functions, encapsulate it in objects as methods that only execute in certain states, or to utilize some core language feature to reduce the code.

From your desire, "I need to ensure that a variable is decremented regardless of how an if clause is exited.." it reads like you want the decrement operation to happen automatically when something else occurs -- a form of encapsulation -- like this:

use strict; my $g_var = 1_000; sub special_dec { my $choice = shift; if ($choice) { --$g_var; } } my $thing = 'dog'; if ( $thing eq 'cat') { my $cntr = 0; while (++$cntr < 11) { last if (special_dec(900==900)); } } elsif ($thing eq 'dog') { my $cntr = 0; while (++$cntr < 11) { last if (special_dec(900==900)); } } print $g_var; 1; # of course prints 999

Now you don't have to think about manually decrementing or handling $g_var, something else does that for you.

Refactoring your solution may mean that you really need to write special operations that allow you to iterate over lists and handle certain internal values without the user of that object having to consciously know about them. I think in Perl there is a desire to try and solve a great many problems using language constructs and coding techniques when oftentimes the more obvious solution is to just push certain actions into functions or object methods instead. If you are doing Perl golf then disregard this post but your question doesn't read like that.

Celebrate Intellectual Diversity


In reply to Re: at continue, last by InfiniteSilence
in thread at continue, last by djerius

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-19 11:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found