Thelonius Monk has asked for the wisdom of the Perl Monks concerning the following question:

Can someone explain or point me to what these two operators' roles are? =~ .=

2006-04-04 Retitled by planetscape, as per Monastery guidelines
Original title: 'What to these operators do?'

Replies are listed 'Best First'.
Re: Explaining =~ and .= operators
by McDarren (Abbot) on Apr 04, 2006 at 01:40 UTC
    You'll find the answer to this in perldoc perlop

    Update: Corrected 2nd example. (thanks lidden & bart)

    =~ binds a scalar expression to a pattern match, eg:

    print "Matches!" if "hello" =~ m/he/;
    The period (.) is a concatenation operator. When used in conjunction with = it concatenates the right argument to the left argument. For example..
    ($foo = "hello") .= " world"; # or $foo = "hello"; $foo .= " world";
    Is the same as..
    $foo = "hello world";

    Cheers,
    Darren :)

      =~ can also be used in list context to obtain the list of captures: $1, $2, etc.

      --

      Oh Lord, won’t you burn me a Knoppix CD ?
      My friends all rate Windows, I must disagree.
      Your powers of persuasion will set them all free,
      So oh Lord, won’t you burn me a Knoppix CD ?
      (Missquoting Janis Joplin)

        Not to be overly nitpicky, but that behavior is from the m// operator, not =~. I think this distinction is important to keep in mind, because =~ can be used with m//, s/// and tr///, which all have different behaviour.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Explaining =~ and .= operators
by sh1tn (Priest) on Apr 04, 2006 at 04:42 UTC