in reply to Re^2: Read text in file and assign to variable
in thread Read text in file and assign to variable

Hello maxamillionk,

Perl is context-sensitive, meaning the return value of a function (or, in this case, of a regular expression match) can change depending on what it's assigned to.

If a match occurs in scalar context, the value returned is the number of matches. In the absence of the /g global modifier, this can only be 1 or 0, success or failure (where failure is indicated by the empty string: ''). my $version = ... puts what follows into scalar context, so the result will be either '1' or '', which is not what you want.

In list context, the contents (i.e. the value) of the match is returned. Putting parentheses around the variable -- my ($version) = ... -- puts what follows into list context, so the result will be the version number matched (if any), which is what you are looking for here.

Hope that helps,

Athanasius <°(((><contra mundum סתם עוד האקר של פרל,

Replies are listed 'Best First'.
Re^4: Read text in file and assign to variable
by maxamillionk (Acolyte) on Jan 25, 2024 at 00:04 UTC
    Thanks Athanasius and kcott. I ended up using ($version) because I consider it more readable.