Once in a Perl script, there is no need to go out to the system to run an external command (that uses Perl's regex!) -- for something Perl can do really well, and really better.
Here are some ways to do this.
Open a file and read line by line, fetching the needed phrase from each line
open my $fh, '<', $file or die "Can't open $file: $!";
my @random_numbers;
while (<$fh>) {
push @random_numbers, /rand_num=(\S+)/g;
}
close $fh;
say for @random_numbers;
Or read all lines at once and pass them through a filter of sorts
open my $fh, '<', $file or die "Can't open $file: $!";
my @random_numbers = map { /rand_num=(\S+)/g } <$fh>;
close $fh;
say for @random_numbers;
I use map to extract the phrase from each line as grep would return whole lines that satisfy the condition. If there is no match an empty list is returned, "disappearing" into the overall return list from map, or into an uninitialized array if nothing whatsoever was matched. This way map acts as a filter.
The above captures the (presumed†) numbers from all rand_num=NUM on each line, if there can be more than one on a line. If also presumes that the filename $file has somehow been read into the script.
Or, use the "null" filehandle (with no need for a filename)
my @random_numbers;
while (<>) { ... } # same block as in first example
# OR
my @random_numbers = map { ... } <>; # same block as in second example
if the script is invoked with that file name on the command line, script.pl file.
This works because the "magic" diamond operator (<>) reads line by line files submitted on the command line. All of them though -- if the script runs as script.pl file1 file2 ... it will do the above through all those files.
One can also read the whole file into a string ("slurp" it) and then work with that. This is helpful when patterns of interest span multiple lines. Let's also use a library, here Path::Tiny
use Path::Tiny; # path()
my @rn = path($file)->slurp =~ /.../g; # same regex as before
Now the regex match is bound to the string returned by the slurp method, which contains the whole file. Since it is done in the list context, imposed on the regex by the list-assignment, all matches are returned in a list, which is assigned to @rn.
I boldly assume that your (every) script starts with
use warnings;
use strict;
For the feature say we also need either use feature 'say'; or to use a version of Perl which includes it or another large framework which does.
† The regex in the question matches all non-whitespace characters following rand_num=. If it is absolutely certain (huh) that these can only be numbers as expected, then so be it.
Otherwise, one could be more careful and use a pattern that will only match an actual number. Or, capture as above and then check that it is indeed a number, using looks_like_number from Scalar::Util
systemdoes not give the program output as return value, it gives the program's exit status. Which is not useful in this case. You need backticks orqx().