in reply to Read text in file and assign to variable

G'day maxamillionk,

Prefer a lexical filehandle in as small a scope as possible. This has these benefits:

There's a variety of ways to achieve this. Here's two:

$ perl -e ' my $file = "pm_11157157_text"; system cat => $file; my $version; { open my $fh, "<", $file; $version = (<$fh> =~ /(\d)/)[0]; } print "Version: $version\n"; ' # CentOS Linux release 7.6.1810 (Core) Version: 7
$ perl -e ' my $file = "pm_11157157_text"; system cat => $file; my ($version) = do { open my $fh, "<", $file; <$fh> =~ /(\d)/; }; print "Version: $version\n"; ' # CentOS Linux release 7.6.1810 (Core) Version: 7

You don't need release in the regex but you may want more than just "7" captured. Again, two possibilities:

$ perl -e ' my $file = "pm_11157157_text"; system cat => $file; my ($version) = do { open my $fh, "<", $file; <$fh> =~ /([0-9.]+)/; }; print "Version: $version\n"; ' # CentOS Linux release 7.6.1810 (Core) Version: 7.6.1810
$ perl -e ' my $file = "pm_11157157_text"; system cat => $file; my ($version) = do { open my $fh, "<", $file; <$fh> =~ /([0-9.]+.+$)/; }; print "Version: $version\n"; ' # CentOS Linux release 7.6.1810 (Core) Version: 7.6.1810 (Core)

— Ken