in reply to Why do we say the =~ operator "binds"?

It just means that the preceeding string or variable will be the lhs of the following m//, s///, or tr/// operator. I would also be interested to know why that's necessary, now that you mention it.

After Compline,
Zaxo

  • Comment on Re: Why do we say the =~ operator "binds"?

Replies are listed 'Best First'.
Re: Re: Why do we say the =~ operator "binds"?
by demerphq (Chancellor) on Apr 18, 2004 at 09:06 UTC

    Essentially regexen are two argument (or three argument or hypothetically four) operators which as far as operators go is pretty weird (note the only common operator like this is the ? : ternary operator. Since the nature of the operator itself has room for, 1, (or two or three depending on how you look at it) additional arguments there needs to be a way to invole the fourth.

    Sorry. to convert all that gobbleygook, look at this:

    $foo =~ s/bar /baz /x EXPR(1) =~ s/REGEX(2)/STRING_OR_EXPR(3)/MODIFIERS(4)

    had the regex operators been expressed as function calls this might have worked as an option set, but notice that in some cases various arguments can be omitted. So if EXPR(1) is omitted $_ is used, in an s/// if REGEX(2) is omitted it uses the last m// pattern on the subject (EXPR(1)), and the modifiers can be omitted. So if this had been positional arguments it would have had a pretty strange argument signature. Try to come up with a reasonable way to pass the arguments into a function based s/// implementation with same behaviour of default operation on $_, and etc. Its not going to be nice, with many common calls using undef in their parameter list. Or alternatively hopelessly verbose like the dotNet regex implementation.

    So in short regex like operators need the binding part because its an optional part (at the front no less) of what is essentially a quaternary operator.

    Also as a last thought....

    my $count=(my $copy=$original)=~s/-(Foo|Bar)-/$1/g;

    :-)


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi


Re: Re: Why do we say the =~ operator "binds"?
by bart (Canon) on Apr 18, 2004 at 18:34 UTC
    It just means that the preceeding string or variable will be the lhs of the following m//, s///, or tr/// operator.
    No it doesn't.

    The string or variable is both in- and (optionally) output of the operation. That puts it in a rather special relationship with regards to the operation, and the language designers chose to name this connection "binding". It's stronger than just input or output.