I'd consider that bad use of a global variable. Whether you
are using OO or not. There are perfectly valid uses for globals,
but to pass data in and out of functions isn't one of them.
A few things to consider:
- What if during you query an exception is thrown? Then
everything that catches an exception has to take care
of cleaning up the global.
- What if you have a more complex operation, and a query
might actually do another query on the tree? With a global,
your algorithm wouldn't be "re-entrant".
There's no good reason to want to use a global here, it's
trivial in Perl to combine lists. Just use recursion and
return a list of answers. Here's an example for doing a range
query on an ordered binary tree:
sub range_query {
my ($node, $from, $to) = @_;
return () unless $node;
return range_query ($node -> left, $from, $to) if $to < $node -
+> key;
return range_query ($node -> right, $from, $to) if $from > $node -
+> key;
return range_query ($node -> left, $from, $to), $node -> key,
range_query ($node -> right, $from, $to)
}
Abigail
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.