Exclude functions from importing #11

Open
berekuk opened this Issue Jun 11, 2012 · 5 comments

Projects

None yet

4 participants

@berekuk
berekuk commented Jun 11, 2012

It would be nice to have a syntax for importing "all functions except one".
Exporter.pm does this with '!' syntax, but unfortunately it's already taken by autodie::hints.
How about '-'?
So, use autodie qw(:all -read) would import everything except read.

This effect is hard to achieve by other means, see https://gist.github.com/2901998 for my attempts to avoid prototype conflicts with a method named read.

@pjf
Owner
pjf commented Jun 13, 2012

I'm currently at YAPC, but just as a quick check, does the following work for you?

use autodie;
no autodie qw(read);
@berekuk
berekuk commented Jun 13, 2012

No, it removes my method for some reason:

mmcleric@domU-12-31-39-0A-60-DD:~$ cat X.pm
package X;

use strict;
use warnings;

use autodie;
no autodie qw(read);

sub read {
    print "foo\n";
}

1;
mmcleric@domU-12-31-39-0A-60-DD:~$ perl -e 'use X; X->read'
Can't locate object method "read" via package "X" at -e line 1.
@mla
Contributor
mla commented Apr 10, 2014

I hit this as well (have a module with a connect() method that is getting clobbered).
I'd like what berekuk suggested would be great.

use autodie qw/ :all -connect /;

@pjf
Owner
pjf commented Apr 10, 2014

Oh, dang.

I agree, autodie qw/ :all -connect /; would be great.

In the tradition of short responses, I'm on the way to KiwiFoo right now, but nudging me in 6-7 days time should reach me in a quiet spot. Alternatively, if you fancy a patch to Fatal::_translate_import_args, I'd be delighted to receive it (especially if it comes with test cases)!

~ pjf

@mla
Contributor
mla commented Apr 11, 2014

I did a bit more poking around and I see what's happening. autodie is doing its work before the user-supplied method is defined which is why unimport is failing. Here's an ugly work-around:

#!/usr/bin/env perl

package Foo;

use strict;
use warnings;

INIT {
  require autodie;
  autodie->import(':all');
  autodie->unimport('connect');
}

sub connect {
  warn "MY CONNECT!\n";
  return;
}

Foo->connect;

With that, we're delaying the autodie setup until after the rest of the code has been defined, which allows unimport to work as expected.

I'll look at putting together a patch for the -connect syntax. There's no way to have autodie automatically delay it's execution until the current file is compiled, is there?

@nthykier nthykier added the wishlist label Oct 4, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment