develooper Front page | perl.perl5.porters | Postings from January 2018

[perl #132732] use if - behaviour does not match documentation

Thread Previous | Thread Next
From:
James E Keenan via RT
Date:
January 26, 2018 02:08
Subject:
[perl #132732] use if - behaviour does not match documentation
Message ID:
rt-4.0.24-7631-1516932475-1590.132732-15-0@perl.org
On Thu, 25 Jan 2018 11:29:38 GMT, abigail@abigail.be wrote:
> On Wed, Jan 24, 2018 at 10:09:56AM -0800, James E Keenan via RT wrote:
> > On Wed, 24 Jan 2018 17:38:14 GMT, grinnz@gmail.com wrote:
> > > On Wed, Jan 24, 2018 at 12:33 PM, James E Keenan
> > > <jkeenan@pobox.com>
> > > wrote:
> > >
> > > > On 01/24/2018 12:20 PM, Dan Book wrote:
> > > >
> > > >> From the source code, 'no if' appears to do the same thing as
> > > >> 'use
> > > >> if'
> > > >> but runs unimport instead of import. It's not a negation of the
> > > >> condition.
> > > >> This is consistent with 'no' and 'use' but the negation is not
> > > >> explained
> > > >> very well IMO.
> > > >>
> > > >>
> > > > So, would a proper test then be to import specific functions,
> > > > then
> > > > use 'no
> > > > if CONDITION, "MODULE" ARGUMENTS to unimport them?
> > > >
> > > >
> > > That should be reasonable, but note that not many modules implement
> > > unimport; 'no warnings' is probably the most common usage of 'no'.
> > >
> > > -Dan
> >
> > Well, the reason I used bigrat in the tests is that it does contain a
> > 'sub unimport'.
> >
> > As does 're'.  But there I get even stranger results.  Try this out
> > (in the patch):
> >
> > #####
> > {
> >     eval "use if (0 > 1), q|re|, qw(is_regexp regexp_pattern);";
> >     ok (! re->can('is_regexp'), "Cannot is_regexp");
> >     ok (! re->can('regexp_pattern'), "Cannot regexp_pattern");
> >     ok (  re->can('is_regexp'), "Can    is_regexp");
> >     ok (  re->can('regexp_pattern'), "Can    regexp_pattern");
> > }
> > #####
> > I get:
> > #####
> > not ok 11 - Cannot is_regexp
> > #   Failed test 'Cannot is_regexp'
> > #   at t/if.t line 73.
> > not ok 12 - Cannot regexp_pattern
> > #   Failed test 'Cannot regexp_pattern'
> > #   at t/if.t line 74.
> > ok 13 - Can    is_regexp
> > ok 14 - Can    regexp_pattern
> > #####
> >
> > Which I read as, "Whether or not the CONDITION evaluates to true or
> > not, the two functions are imported from package 're'."
> >
> 
> 
> That code checks whether the class 're' has a subroutine 'is_regexp'
> --
> which it will as long as it's loaded.
> 
> To check whether it has been imported, you need to call "can" on the
> class functions are imported *to*:
> 
> #####
> {
>     eval "use if (0 > 1), q|re|, qw(is_regexp regexp_pattern);";
>     ok (! main::->can('is_regexp'), "Cannot is_regexp");
>     ok (! main::->can('regexp_pattern'), "Cannot regexp_pattern");
>     ok (  main::->can('is_regexp'), "Can    is_regexp");
>     ok (  main::->can('regexp_pattern'), "Can    regexp_pattern");
> }
> #####
> 
> Assuming your code runs in the main package.
> 
> 
> 
> Abigail

Thanks for the suggestion.  I believe I now have better documentation and testing of 'if'.  Please review the new patch attached, 132732-0001-if-module-clarify-documentation-and-test-more-thorou.patch.

Thank you very much.

-- 
James E Keenan (jkeenan@cpan.org)

---
via perlbug:  queue: perl5 status: open
https://rt.perl.org/Ticket/Display.html?id=132732

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