I have a web interface where users can manage their own population of STB hardware. ... What I want to do is allow users to build their own custom sort statements which they can store in a text file.
This means you'd be giving your users the power to run arbitrary Perl code under whatever user the webserver executes scripts as. So for example, if your web interface has access to a database, you're giving your users the power to access anything in that database that the web interface can, likely including other customer's records. Here you said:
I maintain all instances of the interface
One safer alternative is to give your users a predefined selection of sort orderings. Regexes such as the ones below cover all the cases you showed here. If some other user has yet another custom naming scheme that these orderings don't match, or they want some other arbitrary sort order, then it'd be fairly straightforward for you to add a new set of regexes to the %orderings hash. You would remain in control of the code that gets executed.
use warnings;
use strict;
my @examples = (
[ 'Rack1-Unit2', 'Rack3-Unit1', 'Rack1-Unit1' ],
[ 'R2U3', 'R1U4', 'R10U1', 'R1U1' ],
[ 'R2-U3', 'R1-U4', 'R10-U1', 'R1-U1' ],
);
my $rackre = qr/R(?:ack)?(\d+)/i;
my $unitre = qr/U(?:nit)?(\d+)/i;
my %orderings = (
rackfirst => sub { ($a =~ $rackre)[0] <=> ($b =~ $rackre)[0]
or ($a =~ $unitre)[0] <=> ($b =~ $unitre)[0] },
unitfirst => sub { ($a =~ $unitre)[0] <=> ($b =~ $unitre)[0]
or ($a =~ $rackre)[0] <=> ($b =~ $rackre)[0] },
);
for my $ex (@examples) {
print "Input: @$ex\n";
for my $o (sort keys %orderings) {
my @sorted = sort {&{$orderings{$o}}} @$ex;
print "$o: @sorted\n";
}
}
__END__
Input: Rack1-Unit2 Rack3-Unit1 Rack1-Unit1
rackfirst: Rack1-Unit1 Rack1-Unit2 Rack3-Unit1
unitfirst: Rack1-Unit1 Rack3-Unit1 Rack1-Unit2
Input: R2U3 R1U4 R10U1 R1U1
rackfirst: R1U1 R1U4 R2U3 R10U1
unitfirst: R1U1 R10U1 R2U3 R1U4
Input: R2-U3 R1-U4 R10-U1 R1-U1
rackfirst: R1-U1 R1-U4 R2-U3 R10-U1
unitfirst: R1-U1 R10-U1 R2-U3 R1-U4
(Note a Schwartzian transform could also be used to improve performance.)