Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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

by diotalevi (Canon)
on Apr 18, 2004 at 04:39 UTC ( #346057=note: print w/replies, xml ) Need Help??

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

$var = /$pattern/; probably isn't a bug. I've writen that plenty of times. I'd hate for people to read that and think I didn't actually mean assignment.

Replies are listed 'Best First'.
Re: Re: Re: Why do we say the =~ operator "binds"?
by William G. Davis (Friar) on Apr 18, 2004 at 09:13 UTC
    I'd hate for people to read that and think I didn't actually mean assignment.

    Then don't write it that way:

    foreach my $address (@email_addresses) { my $is_valid = $address =~ /[a-zA-Z0-0.]+\@[a-zA-Z0-0.]+/; if ($is_valid) { .... } }

    Seriously, if you're writing code for public consumption and think, "Gee, I hope who's ever reading this can understand it," then rewrite it so you're sure they'll be able to understand it, or comment it, or do both.

      I just grepped my source repository to see where I'd written that and I couldn't find it. The general case I'd thought of where it was annoying or inconvenient to the narrative to give the topicalized variable a name. I can't find an example (though I did just find a code-typo in where ~= was said instead of =~).

      for ( ... ) { $foo = /.../; }

        It's *almost never* annoying or inconvenient to the narrative to give topicalized variables names.

        The loop is the discussion and the alias variable is what's being talked about. It's the topic--the subject--and it has a name, even if you don't feel like giving it one. And using "for" when you really mean "foreach" element in some list doesn't help anyone either (again, when the code's for public consumption, not when it's just for you.)

      Well, I not only wouldn't use explicit loop variables when I can resort to the singular pronoun (or even better, when I can omit it) but I would also try to avoid unnecessary assignments ;-)
      foreach (@email_addresses) { if ( /[a-zA-Z0-0.]+\@[a-zA-Z0-0.]+/ ) { .... } }
      This is IMHO more legible, as long as your public is fluent in Perl, or just knows the basics.
      The only context where I would use your code is a Perl class, but just to show immediately after how to shorten that code and make it more legible (confess, you are a teacher! ;-)


        Emazep, it was just an example; Diotalevi and I were talking about a situation in which the assignment is practical for some reason (like if you're calling m//g in scalar context), not one where you can get away with sticking m// in a conditional by itself. (Who on earth would store away the result unless they needed for something later on, anyway?)

        My point was that if you have an assignment like "$var = /pattern/", then name the variables and use the binding operator explicitly so it's completely unambiguous: "$pos = $string =~ /pattern/g".

        In real life I would have written that validity checking code like this:

        foreach my $address (@email_addresses) { return unless ($address =~ /[a-zA-Z0-0.]+\@[a-zA-Z0-0.]+/); # proceed normally...
Re: Re: Re: Why do we say the =~ operator "binds"?
by demerphq (Chancellor) on Apr 18, 2004 at 09:11 UTC

    As long as $var was named something like $count, or possibly $matched, or the context itself gave this information then I would say its fine. If $var was actually $str or the like I would probably say it was a bug. If it said ($var)=/$pattern/ it would almost certainly be fine regardless.


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://346057]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2018-06-24 01:19 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.