Fellow Monks,

Due to recent prodding by lestrrat i decided to take up some code i had been writing that solves the nqueens problem. Eventually this code will get a Tk interface (hopefully using the nifty new module from clintp) and become a screensaver. The algorithms that do the behind the scenes stuff are almost done, and when implementing the last major optimization i ran into a brick wall.

This is implemented using OOP and i decided that it would make the most sense to have the chessboard and the queens be objects. The recent optimization involved moving some code that checks all the contested values for all the queens from main into Board.pm; now i run this code once directly after set-up, as opposed to in between every queen swap (there's a new code snippet in main to deal with changed contested values that runs in O(N) time :). In main it worked fine, but in Board.pm it fails in one interesting way. contested is a method in Queen.pm defined as thus:

sub contested : lvalue { my $self = shift; $self->{_contested} }
And that has proven to work fine. When the code to determine contentions was in main all of the values got updated appropriately. Now that i have moved this to Board though i have found that it will get the value for contested, but not set it.

When going through Board.pm to find out why i ran into another interesting quandery; i had not required the Queen object in Board, yet it made new Queens, changed their location and other things anyway. The require line has since been added to Board.pm (and i smacked myself for forgetting it) but it did not fix any problems. So, with that set up, here is my snippet for setting the number of contentions (for reference) quickly followed by my questions:

# works in package main; but not package Board; foreach my $queen1 (@queens) { LOOP: foreach my $queen2 (@queens) { next LOOP if $queen1->duplicate($queen2->location); $queen1->contested++ if $linear->($queen1->location, $queen2->lo +cation); } }
  1. Does Perl not need require statements on module A inside of module B if module A is required in main before module B? If so i think this is probably a misfeature
  2. Is there another way to explain why i didn't need to require the Queen module in Board.pm and still got to use all of it's features?
  3. Most importantly, why can i not set the lvalue correctly? For example:
    $queen1->contested++
    no longer works, when it worked (and works) fine in the main package.
Thanx for any help,
jynx

In reply to Why does my method not lvalue properly? by jynx

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.