Re: Re: Why do we say the =~ operator "binds"?
by diotalevi (Canon) on Apr 18, 2004 at 04:39 UTC
|
$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.
| [reply] [d/l] |
|
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. | [reply] [d/l] |
|
for ( ... )
{
$foo = /.../;
}
| [reply] [d/l] |
|
|
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! ;-)
Cheers,
Emanuele. | [reply] [d/l] |
|
|
|
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.
---
demerphq
First they ignore you, then they laugh at you, then they fight you, then you win.
-- Gandhi
| [reply] [d/l] |
Re: Re: Why do we say the =~ operator "binds"?
by Wassercrats (Initiate) on Apr 18, 2004 at 08:16 UTC
|
For $var =~ /pattern/, I'd prefer "$var is associated with /pattern/" or for a substitution: "/pattern/ acts upon $var" could be used. Binds makes it sound like a more permanent or default association. Bind would be appropriate when saying "/pattern/ binds with $_." | [reply] [d/l] |
|
| [reply] [d/l] [select] |
|
If "=~ is the pattern applying operator", then code like if( /foo/ ) would never apply the regex. The two sides of =~ are *not* passed to some built-in function as arguments. The right-hand side is what is run and the left-hand side is the argument if it is present. An "unbound" regex still gets applied. Matching happens even without =~.
The =~ (mostly) is *not* what makes pattern matching happen. The regex is what makes pattern matching happen and the =~ (if present) binds a string to the regex to tell it what to match against (other than the default).
Now, if you have =~ with something that isn't a regex to its right, then you have rather bizarre code and are at the mercy of dubious "do what I mean" guessing. If Perl happens to currently insert a regex constructor for you, my main reaction is that I know I have no code that relies on such.
So the reason we say 'bind' is because one side is the code to run. Like '(' is used to bind arguments to a function -- '(' is not "the function invoking operator", despite that description being somewhat appropriate.
| [reply] [d/l] |
|