Probably you'll get better advice but this was a fun and straightforward little problem so-

package MyRanger; use strict; use Carp; sub new { my $caller = shift; my $self = bless {}, $caller; $self->{_range_string} = shift || croak "You must provide a range" +; for my $entry ( split /\s*,\s*/, $self->{_range_string} ) { if ( $entry =~ /\A\d+\z/ ) { push @{$self->{_raw_range}}, $entry; } elsif ( $entry =~ /\A(\d+)-(\d+)\z/ ) { carp "Range '$entry' is impossible, skipping" and next if $1 > $2; $entry =~ s/-/../; push @{$self->{_raw_range}}, eval $entry; } else { croak "Illegal range entry '$entry' given"; } } @{$self->{_range}} = sort { $a <=> $b } keys %{ { map {$_ => 1} @{ +$self->{_raw_range}} } }; @{$self->{_range}} || croak "No range was parseable in ", $self->{ +_range_string}; $self->{_pos} = 0; $self; } sub next { my $self = shift; $self->{_range}->[$self->{_pos}++]; } 1; my @range_strings = ( "1, 3, 5-7", "1-5, 2, 4, 10, 100", "1-3, 2-5", "12-10", ); for my $string ( @range_strings ) { print "MyRanger->new with '$string'\n"; my $ranger = MyRanger->new($string); while ( my $item = $ranger->next() ) { print "\tNext item is: $item\n"; } }

Extra stuff like-

sub size { my $self = shift; scalar @{$self->{_range}}; }

...d'oh, left up to you. :)


In reply to Re: Perl Module - take in a String, output object by Your Mother
in thread Perl Module - take in a String, output object by MikeEndo

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.