Open p5pRT opened 8 years ago
This program contains a mistake:
% perl -wE '$re = qr/a/; $s = "abc"; if ($re =~ $s) { say "hi" }'
The match operator is the wrong way round; it should be $s =~ $re. This is the kind of mistake that can be hard to see even if you stare at the code for a long time!
If warnings are enabled\, and the LHS of the =~ operator is a regular expression object (ref $foo eq 'Regexp')\, and the RHS is not a literal regexp or a regular expression object\, then a warning should be produced.
use warnings; my $re = qr/f/; my $re2 = qr/g/; my $s = 'foo';
$s =~ $re; # OK $re =~ $s; # warns $re =~ /x/; # does not warn since RHS is a literal regexp $re =~ $re2; # does not warn since RHS is a regexp object
The second and third cases above *could* warn\, but for the purpose of this bug report I have left them out since I want to narrow down the warning as much as possible and minimize false positives.
On Thu\, Sep 22\, 2016 at 08:38:55AM -0700\, Ed Avis wrote:
This program contains a mistake:
% perl \-wE '$re = qr/a/; $s = "abc"; if \($re =~ $s\) \{ say "hi" \}'
Who says it's a mistake? I've certainly written code which introspects patterns by matching a pattern against it.
And I don't always write /$pattern/ on the RHS of a =~ operator. After all\, Perl is there for the programmer; the programmer isn't there for Perl. Perl knows I mean a pattern\, because I write =~\, and will duely treat a string as a pattern.
Abigail
The RT System itself - Status changed from 'new' to 'open'
* Abigail \abigail@​abigail\.be [2016-09-22 22:00]:
On Thu\, Sep 22\, 2016 at 08:38:55AM -0700\, Ed Avis wrote:
This program contains a mistake:
% perl \-wE '$re = qr/a/; $s = "abc"; if \($re =~ $s\) \{ say "hi" \}'
Who says it's a mistake? I've certainly written code which introspects patterns by matching a pattern against it.
Iâve done that\, but extremely rarely. I wonder if warning the user when they use a qr object on the LHS might be worthwhile. You can always and easily stringify it if thatâs what you really want.
And I don't always write /$pattern/ on the RHS of a =~ operator.
strict.pm and warnings.pm themselves do that. :^) Courtesy of yours truly. :^P (Because constant-folding.)
Not gonna fly.
Regards\, -- Aristotle Pagaltzis // \<http://plasmasturm.org/>
* Aristotle Pagaltzis \pagaltzis@​gmx\.de [2016-09-26 06:48]:
I wonder if warning the user when they use a qr object on the LHS might be worthwhile. You can always and easily stringify it if thatâs what you really want.
No.
Any time a value crosses an authority boundary\, like when a value is passed from user code to a CPAN module\, the code on the other side of the boundary would have to defensively stringify the LHS in every last pattern match against that value\, or else go to the effort to propagate the warning back to the boundary point.
Not gonna fly.
Not as part of perl proper. However:
package Regexp { use overload q[""] => sub { die } }
Looks like that does what youâd think itâll do. And returning $_[0] doesnât seem to cause recursion\, so getting a warning out of it is just as easy.
So anyone who wants as much can already have it.
Regards\, -- Aristotle Pagaltzis // \<http://plasmasturm.org/>
Aristotle Pagaltzis wrote:
Any time a value crosses an authority boundary\, like when a value is passed from user code to a CPAN module\, the code on the other side of the boundary would have to defensively stringify the LHS in every last pattern match against that value\,
This is true. But the proposal was to warn only if the LHS of =~ is a regexp and the RHS is not. Take the following code
sub match_and_report { my ($string\, $regexp\, $message) = @_; if ($string =~ $regexp) { say "it matches\, $message" } }
Now\, if this code is called the wrong way round\, transposing the string and regexp arguments\, isn't it reasonable for it to warn? Even if the warning is seen by the programmer as coming from inside the bowels of match_and_report\, rather than being a nicely packaged 'carp' of the caller's mistake. Even though the mistake is by the caller\, and there is no bug in match_and_report itself\, the warning is still helpful in practice to the programmer.
This is surely analogous to the warnings on uninitialized values. Every time a value crosses into a module's code\, it 'would have to defensively check against undef'. But in fact perl often gives 'Use of uninitialized value' coming from inside library code. The programmer then can peek inside the library to work out what's wrong. High-quality libraries will often have defensive usage checks for the most common errors\, but it's unlikely to be entirely free of warnings in all cases where the caller might pass undef. Again\, it's not a perfect diagnostic tool\, since the line number reported in the warning is a long way off where the undef was passed\, but nobody suggests perl shouldn't warn on undef values at run time for this reason.
I suggest that using =~ 'the wrong way round' is if anything more likely to be a mistake than doing something like undef()+5\, and is at least as worthy of a runtime warning\, bearing in mind that warnings can be turned on and off.
-- Ed Avis \eda@​waniasset\.com
This email is intended only for the person to whom it is addressed and may contain confidential information. Any retransmission\, copying\, disclosure or other use of\, this information by persons other than the intended recipient is prohibited. If you received this email in error\, please contact the sender and delete the material. This email is for information only and is not intended as an offer or solicitation for the purchase or sale of any financial instrument. Wadhwani Asset Management LLP is a Limited Liability Partnership registered in England (OC303168) with registered office at 9th Floor Orion House\, 5 Upper St Martinâs Lane\, London\, WC2H 9EA. It is authorised and regulated by the Financial Conduct Authority.
Migrated from rt.perl.org#129333 (status was 'open')
Searchable as RT129333$