In addition to the excellent comments by
castaway above, I would only add that it is additionally advantageous to include an escape clause within the recursive subroutine in order to ensure that the parsing routine does pass beyond a specified recursion depth - This is a relatively simple safeguard to implement from a defensive programming standpoint.
For example, a one-liner which (eventually) exhausts the memory available to the Perl process:
rob@development:/home/rob/workspace$ perl -e 'sub a { &a }; a'
Out of memory!
This can be prevented by the incorporation of a maximum depth for subroutine recursion such as follows:
sub parsevcard {
my ($inputstr, $inputpos, $depth) = @_;
if ($depth > $max_depth) {
# Recursion beyond allowable depth error handling here
}
.
.
$currentcard->{AGENT} = parsevcard($inputstr, $pos, ++$depth);
}
Depending upon the recursive problem at hand, this error trapping can be performed by way of examination of the output data structure alone, for example, the number of elements within an output array, rather than by a separate counter per se.
perl -le "print+unpack'N',pack'B32','00000000000000000000001010011110'"
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.