You describe your problem as :

Each new loop that I wrapped the original code in required resetting some variables, which I was able to handle, but then I added another loop, to make a single instance of the script run over and over for different clients. Once one job is done, it waits for another request. I could have tried figuring out what variables needed to be cleared, but I could easily have missed something.

This is exactly the problem that block scoped ("lexical") variables, that is, variables declared with my solve for you:

use strict; my @sites = qw(http://www.corion.net); my $user_name = "Corion"; for my $user_site (@sites) { print "Summary for site $user_site\n"; my $link_number; # = 0; # left the initialization out intentionally for my $link (get_site_links($user_site)) { print "$link_number: Found link $user_site$link\n"; $link_number++; }; }; sub get_site_links { return qw(index.html test.html error.html); };

Here, there is no need to reset the variables outside of their usage, as they won't be available, and for example $link_number will be reset to zero by Perl every time a new site loop is started. use strict; is there so that Perl can check for me whether I've used any of my block scoped variables only within the block I've intended to use them.

So, Perl already provides you with the tools, but you also have to use the right tool for the right problem...

Another, less good solution for your problem might be the local keyword, which allows you to temporarily assign a different value to a variable and Perl automagically restores the previous value when you leave the block of the local declaration, but for the problem you specified, lexical variables are the best solution.


In reply to Re: Re: Re: Re: Re: Re: Re: VarStructor 1.0 by Corion
in thread VarStructor 1.0 by Wassercrats

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.