develooper Front page | perl.perl5.porters | Postings from May 2013

Re: [perl #117969] use POSIX emits multiple warnings if used after"Cwd"

Thread Previous | Thread Next
From:
Lukas Mai
Date:
May 12, 2013 18:02
Subject:
Re: [perl #117969] use POSIX emits multiple warnings if used after"Cwd"
Message ID:
518FD901.4070806@gmail.com
On 12.05.2013 19:10, Linda Walsh via RT wrote:
> On Sat May 11 23:17:18 2013, plokinom@gmail.com wrote:
>> No, that's normal. You're loading two modules, each of which redefines
>> main::getcwd().
> ---
> The idea that that the POSIX module would blindly redefine common
> functions is entirely a short-coming in the POSIX module.

No, that's the entire reason POSIX (and modules in general exist): to 
define common functions.

>>> Second -- is it's not easy to shut them off.
>>
>> It's trivial to shut them off: Just remove -w.
>> There is no reason to use -w in any perl that supports 'use warnings'
>> (i.e. 5.6 or better).
> ----
>       It turns out that putting the CWD module 2nd silences the problem
> as it doesn't have the same bug as the POSIX module.  If the warning
> happened in both instances, I'd say you have a point.  But the fact that
> it doesn't means that the POSIX module it redefining things that are
> already defined when it has not been asked to.


C:\>perl -we "use POSIX; use Cwd;"
Subroutine main::getcwd redefined at D:/strawberry/perl/lib/Exporter.pm 
line 66.
  at -e line 1.

1. Putting Cwd (not CWD) second doesn't silence the warning.
2. This is not a bug in POSIX.
3. You did in fact ask POSIX to redefine getcwd because that's what 'use 
POSIX;' means.

>       I usually support modules, like POSIX, exporting all of their
> needed routines, but with large export lists, it becomes even more
> important not to do so blindly, but check to see if the module is
> already defined in the user's context and only issue warnings for those
> modules that the user has specifically requested
> (as in use  POSIX qw(getcwd) ).

getcwd is a subroutine, not a module.

No module I know explicitly checks whether the symbols it exports are 
already defined. As I said before, if you wanted that to happen, the 
appropriate place would be in Exporter.

If you want 'use POSIX qw(getcwd);' to do something different than 'use 
POSIX;', that would be non-standard behavior. That is, it would require 
custom code in POSIX that is more than just a wrapper around Exporter. 
It would also behave differently than every other Perl module. And it 
would be an incompatible change to the existing behavior/documentation. 
All in all it sounds very unintuitive to me.

How would this interact with import tags?
For example, 'use POSIX qw(:assert_h);' exports 'assert' and 'NDEBUG'. 
Does that count as specifically requesting assert and NDEBUG or not?

> That's a reasonable approach.
>
> Second -- I clearly stated that in most of my programs putting
> a "no warnings" before something that is emitting warnings HAS
> overridden the specification of "-w" on the interpreter line.

True but irrelevant. In this case the something that is emitting 
warnings is in Exporter.pm, not in your code, and there is no 'no 
warnings' in Exporter.pm.

> To go off and tell me why that doesn't work flied in the face of plenty
> of other examples.
>
> Should I report the 20-score or more cases where it does work as bugs?

I have no idea what you're talking about here.

>
>>> For one reason or another, the
>>> 'no warnings'                in front of the
>>> 'use POSIX'                  doesn't turn them off.
>>
>> The reason is that 'use warnings'/'no warnings' is lexical, i.e. it only
>> affects code in the current block. The actual subroutine redefinition,
>> however, happens in the POSIX module (or probably in Exporter, even).
>> There's no way to influence warnings from other modules via 'use
>> warnings'/'no warnings'.
> ---
>
> See above...  I'm now wondering about the majority of the cases
> where it does work... and wondering why...

What do you mean by "work"? As far as I can see, your code works as 
expected (i.e. it does what it's documented to do).

> FWIW.. in my usage case, require POSIX was all that was needed,
> I didn't rely on any exported names (as I didn't know that they were
> exported let alone, that one of them conflicted with Cwd or that they
> would stomp on the namespace).

The second paragraph of 'perldoc POSIX' states: "*Everything is exported 
by default* with the exception of ...".

If you want to load a module without importing anything, you can write 
'use Some::Module ();'. This is explained in 'perldoc -f use'.

By "stomp on the namespace" do you mean "export symbols like every other 
module"?

> The fact that it stomps on namespace and issues warnings for things as
> it overwrites things that have not been explicitly imported seems an
> unfriendly default for a CORE module, no?

This is not a fact. What you're describing is not the default behavior 
of anything.

-- 
Lukas Mai <plokinom@gmail.com>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About