Perl, assign to a variable from a regular expression

I'm relatively new to perl, and there is an example code snippet in check_ilo2_health.pl that has a syntax snippet that I don't understand how or why this works. The code snippet processes the SSL client data, in this case XML, line by line.

if ( $line =~ m/MESSAGE='/) {
   my ($msg) = ( $line =~ m/MESSAGE='(.*)'/);  #<---- HERE

   if ( $msg !~ m/No error/ ) {
      if ( $msg =~ m/Syntax error/ ) {  #...etc

An example of the XML in question:

<RESPONSE
    STATUS="0x0000"
    MESSAGE='No error'
 />

Thus, in this case, the if statement accepts the MESSAGE string of the XML sample. I understand that my ($ msg) treats the variable as a kind of list, and I understand how regular expressions match; however, I do not understand the syntax such that $ msg is assigned without errors. Perl seems to play with the syntax in parentheses, and it works for that. While it works, I would like to know how it works. Any help would be appreciated.

+5
source share
1 answer

See Perlretut, Extracting-matches :

... in a scalar context, $time =~ /(\d\d):(\d\d):(\d\d)/returns true or false. However, in the context of the list, it returns a list of matching values($1,$2,$3)

So in

($msg) = ( $line =~ m/MESSAGE='(.*)'/);

( $line =~ m/MESSAGE='(.*)'/) . , ($ msg).

+10

All Articles