Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

warnings::enabled($object) doesn't behave as documented #10128

Open
p5pRT opened this issue Feb 3, 2010 · 4 comments
Open

warnings::enabled($object) doesn't behave as documented #10128

p5pRT opened this issue Feb 3, 2010 · 4 comments

Comments

@p5pRT
Copy link

p5pRT commented Feb 3, 2010

Migrated from rt.perl.org#72506 (status was 'open')

Searchable as RT72506$

@p5pRT
Copy link
Author

p5pRT commented Feb 3, 2010

From @nwc10

warnings​::enabled is documented as this​:

=item warnings​::enabled($category)

Return TRUE if the warnings category, C<$category>, is enabled in the
calling module.
Otherwise returns FALSE.

=cut

However, it only works as documented in simple cases. All 3 of these should
warn​:

#!perl
use strict;
no warnings;

package Foo;

use warnings::register;

sub method {
my $self = shift;
warnings::warnif($self, 'Invocant');
}

sub subroutine {
my ($a, $b) = @_;
warnings::warnif($b, 'Second');
}

sub funky {
my $self = shift;
my $r = \@_;
warnings::warnif($self, 'Funky');
}

package main;

use warnings 'Foo';

my $var = bless [], 'Foo';

$var->method();
Foo::subroutine(undef, $var);
$var->funky();
__END__

However, only the first does;

$ perl warner.pl
Invocant at warner.pl line 31
$

This is because the implementation relies on @​DB​::args​:

if ($isobj) {
while (do { { package DB; $pkg = (caller($i++))[0] } } ) {
last unless @DB::args && $DB::args[0] =~ /^$category=/ ;
}
$i -= 2 ;
}

It simply can't work (ever) as documented for all but the simplest cases.
I don't like this state of affairs.

I suspect that the "useful" intent here is to warn based on the lexical state
at the point of the method call. That *can* be done reliably, using the
packages returned from caller.

Nicholas Clark

@p5pRT
Copy link
Author

p5pRT commented Feb 4, 2010

From @nwc10

On Wed, Feb 03, 2010 at 02​:30​:46AM -0800, Nicholas Clark wrote​:

warnings​::enabled is documented as this​:

=item warnings​::enabled($category)

Return TRUE if the warnings category, C<$category>, is enabled in the
calling module.
Otherwise returns FALSE.

=cut

Nick's a muppet. I meant to grab​:

=item warnings​::enabled($object)

Use the name of the class for the object reference, C<$object>, as the
warnings category.

Return TRUE if that warnings category is enabled in the first scope
where the object is used.
Otherwise returns FALSE.

=cut

Then this makes sense​:

However, it only works as documented in simple cases. All 3 of these should
warn​:

It simply can't work (ever) as documented for all but the simplest cases.
I don't like this state of affairs.

I suspect that the "useful" intent here is to warn based on the lexical state
at the point of the method call. That *can* be done reliably, using the
packages returned from caller.

On CPAN, it's only used by

Graphics-MNG/MNG.pm
Mac-iPod-GNUpod/GNUpod.pm
Object-Meta-Plugin/lib/Object/Meta/Plugin/Useful/Generic.pm
Text-Sprintf-Named/lib/Text/Sprintf/Named.pm

As best I can tell, none of the above would notice if the definition and
implementation changed as proposed.

accessors-fast/lib/accessors/fast.pm passes a parameter of $ME, but that's
set once in a BEGIN block to __PACKAGE__. (No, I don't understand this - $ME
isn't documented as something you can change, so why not keep the constant?)

perl_mlb/warnings.pm is a copy of the perl library.

warnings-compat/t/warnings.t tests warnings.pm

Nicholas Clark

@p5pRT
Copy link
Author

p5pRT commented Dec 12, 2010

From @cpansprout

Now (as of 5.10.0) that warnings.pm uses Carp to determine the call
site, should we not just switch objects over to using Carp, which does
more or less was I think the warnings code was intended to do?

@p5pRT
Copy link
Author

p5pRT commented Dec 12, 2010

The RT System itself - Status changed from 'new' to 'open'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants