Skip to content

Instantly share code, notes, and snippets.

@rurban
Last active August 3, 2018 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rurban/e7e3873613c4658bc552fba4356d7022 to your computer and use it in GitHub Desktop.
Save rurban/e7e3873613c4658bc552fba4356d7022 to your computer and use it in GitHub Desktop.
use warnings 'shadow-package'
# detected by mziescha
use warnings 'shadow-package'; # or just shadow
package Acme::Test;
sub new { bless {}, shift }
sub Sub { 'shadowed' }
package Acme::Test::Sub;
sub new { print 'Sub'; bless {}, shift } # warn: "Package Acme::Test::Sub shadowed by Acme::Test->Sub at warn-shadow-pkg.pl, line 8"
package main;
# calls &Acme::Test:: Sub, then ->new, not &Acme::Test::Sub -> new
Acme::Test::Sub->new(); # warn: "Subroutine &Acme::Test::Sub shadowed by Acme::Test->Sub at warn-shadow-pkg.pl, line 13"
=pod
Note that Acme::Test::Sub is written in package and subroutine syntax it is still
taken as static method call, overriding the existing package.
With use warnings 'shadow-package' or 'shadow'
warn about when
* adding a new package shadowed by an existing method (at compile-time)
* adding a method shadowing an existing package (at compile-time)
* calling a method shadowing an existing package-sub (at compile-time)
No warning at run-time? Or only when not already warned before.
This would be needed for computed method names, if it still shadows the package then.
tracked at https://github.com/perl11/cperl/issues/368
=cut
@rurban
Copy link
Author

rurban commented Aug 3, 2018

Open points:

  • In the calling case. Use which location? The callers location to see the wrong usage, or the definition of the shadower: file, line 6 or both? "&Acme::Test::Sub shadowed by Acme::Test->Sub defined at warn-shadow-pkg.pl, line 6 at warn-shadow-pkg.pl, line 13", na

  • name of the warning? it should be in the shadow category. so when you use warnings 'shadow'; it should be enabled.
    the other case of shadow warnings would be variables being shadowed. These are not so severe as packages.

  • compile-time only, or also at run-time? In C such warnings are compile-time only (of course). But compile-time only would miss all the cases with computed method-names, which could also be shadowed.

Tracked at perl11/cperl#368

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment