use strict;
use warnings;
my $string = '1234 this is the remaining string';
# A4 = take the first 4 ASCII characters
# x = skip the next byte
# A* = take all remaining ASCII characters
my ( $num, $text ) = unpack( 'A4xA*', $string );
print "[$num][$text]\n";
####
[1234][this is the remaining string]
####
use strict;
use warnings;
use Benchmark qw( cmpthese );
my $string = '1234 this is the remaining string';
cmpthese( -5, {
unpack => sub { unpack( 'A4xA*', $string ) },
regex => sub { $string =~ m[(\d+)\s+(.*)$] },
split => sub { split( /(\d+)\s+/, $string, 2 ) },
} );
####
Rate split regex unpack
split 789472/s -- -44% -70%
regex 1422218/s 80% -- -45%
unpack 2594984/s 229% 82% --