Purple::Sequence - Generate the next NID in a Sequence of NIDs


Purple documentation Contained in the Purple distribution.

Index


Code Index:

NAME

Top

Purple::Sequence - Generate the next NID in a Sequence of NIDs

SYNOPSIS

Top

This module contains the code for calculating the next NID in a sequence of Purple Numbers.

    my $next_nid = Purple::Sequence::increment_nid($current_nid);

FUNCTIONS

Top

increment_nid($current_nid)

Returns the next NID after $current_nid.

AUTHORS

Top

Chris Dent, <cdent@burningchrome.com>

Eugene Eric Kim, <eekim@blueoxen.com>

BUGS

Top

Please report any bugs or feature requests to bug-purple@rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Purple. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

COPYRIGHT & LICENSE

Top


Purple documentation Contained in the Purple distribution.

package Purple::Sequence;

use strict;
use warnings;

sub increment_nid {
    my $old_nid = shift;

    my @oldValues = split('', $old_nid);
    my @newValues;
    my $carryBit = 1;

    foreach my $char (reverse(@oldValues)) {
        if ($carryBit) {
            my $newChar;
            ($newChar, $carryBit) = _incChar($char);
            push(@newValues, $newChar);
        } else {
            push(@newValues, $char);
        }
    }
    push(@newValues, '1') if ($carryBit);
    return join('', (reverse(@newValues)));
}

sub _incChar {
    my $char = shift;

    if ($char eq 'Z') {
        return '0', 1;
    }
    if ($char eq '9') {
        return 'A', 0;
    }
    if ($char =~ /[A-Z0-9]/) {
        return chr(ord($char) + 1), 0;
    }
}

1;

1; # End of Purple::Sequence