Indeed, that’s a scenario where you really really don’t want to use eval.

How constrained is the format? If sufficiently so, how about parsing the input manually? Assuming the dots and commata are the only “metacharacters”, you could do something like this:

my @range = map { m{ (\d+) \s* \.\. \s* (\d+) }msx ? $1 .. $2 : /(\d+)/ } split /,/, $range;

This breaks the string apart at the commata, then tries to match something that looks like a range; if it succeeds then it uses the regular range operator to make a real range out of it. If it fails, it returns the first sequence of digits it finds.

It should work correctly for any combination of ranges and single numbers strung together with commata. It’s also pretty lenient and will accept whitespace within ranges and any sort of non-digit garbage everywhere else – that might not be what you want, or might be just fine. Your call.

It will also not work for negative numbers and for fractional components. If you need to match more than just positive integers, the quickest and most robust way is probably to employ Regexp::Common.

Makeshifts last the longest.


In reply to Re^3: Iterate string like, for(1..5) i.e. $stirng="1..5"; for($string) by Aristotle
in thread Iterate string like, for(1..5) i.e. $stirng="1..5"; for($string) by gman

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.