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

setting CPAN_OPTS environment variable fails #131

Open
stuart-little opened this issue Jan 2, 2020 · 5 comments
Open

setting CPAN_OPTS environment variable fails #131

stuart-little opened this issue Jan 2, 2020 · 5 comments

Comments

@stuart-little
Copy link

stuart-little commented Jan 2, 2020

I'm running:

${HOME}/perl5/bin/cpan version 1.64 calling Getopt::Std::getopts (version 1.12 [paranoid]),
running under Perl version 5.28.1.

I have been trying to run installs without testing by setting the CPAN_OPTS environment variable as documented, e.g. here (as well as in the cpan manpages on all of my machines). The problem is that the contents of that variable seem to be appended rather than prepended to the rest of my arguments.

The three commands

cpan CPAN -T
CPAN_OPTS="-T" cpan CPAN
export CPAN_OPTS="-T" && cpan CPAN

have exactly the same effect:

Loading internal logger. Log::Log4perl recommended for better logging
Reading '${HOME}/.cpan/Metadata'
  Database was generated on Thu, 02 Jan 2020 14:41:03 GMT
CPAN is up to date (2.27).
>(error): Could not expand [-T]. Check the module name.
>(info): I can suggest names if you install one of Text::Levenshtein::XS, Text::Levenshtein::Damerau::XS, Text::Levenshtein, and Text::Levenshtein::Damerau::PP
>(info): and you provide the -x option on invocation.
>(error): Skipping -T because I couldn't find a matching namespace.

In retrospect, it should be easy to see why this would happen. In the cloned git repo:

$ grep -rE '_OPTS' ./

./scripts/cpan:=item CPAN_OPTS
./lib/App/Cpan.pm:=item CPAN_OPTS
./lib/App/Cpan.pm:      push @ARGV, grep $_, split /\s+/, $ENV{CPAN_OPTS} || '';

That last line pushes instead of unshifting. But then how are any of these options supposed to work?

@andk
Copy link
Owner

andk commented Jan 2, 2020 via email

@stuart-little
Copy link
Author

@andk: I understand cpan -T <package> works; it does for me too! The problem is it doesn't work if the -T comes after the package name, and that's exactly what setting CPAN_OPTS="-T" does.

So again: the problem isn't with the -T option, but with the CPAN_OPTS variable, which is appended to the package name instead of being prepended.

I don't think there's much to understand: the problem is, as indicated in my edit to the original message, in the line

push @ARGV, grep $_, split /\s+/, $ENV{CPAN_OPTS} || '';

in lib/App/Cpan.pm: the push means the CPAN_OPTS contents are added to the end of @ARGV, when in fact they should be added to the beginning with an unshift.

@stuart-little
Copy link
Author

Actually, that's literally the only change needed to make CPAN_OPTS work as advertised. I simply opened my

${PERL5LIB}/App/Cpan.pm

and changed

push @ARGV, grep $_, split /\s+/, $ENV{CPAN_OPTS} || '';

to

unshift @ARGV, grep $_, split /\s+/, $ENV{CPAN_OPTS} || '';

Now everything works! The command export CPAN_OPTS="-T" && cpan CPAN now adds the -T to the beginning of the @ARGV passed to cpan, and turns off the testing as desired.


In short, I would propose the following patch:

--- a/lib/App/Cpan.pm
+++ b/lib/App/Cpan.pm
@@ -410,7 +410,7 @@ sub _process_options
        {
        my %options;
 
-       push @ARGV, grep $_, split /\s+/, $ENV{CPAN_OPTS} || '';
+        unshift @ARGV, grep $_, split /\s+/, $ENV{CPAN_OPTS} || '';
 
        # if no arguments, just drop into the shell
        if( 0 == @ARGV ) { CPAN::shell(); exit 0 }

@andk
Copy link
Owner

andk commented Jan 2, 2020 via email

@stuart-little
Copy link
Author

Have you noticed, that there is no test in the t/ directory dealing with
CPAN_OPTS:

I hadn't noticed that, no.

If no usage whatsoever of CPAN_OPTS works at the moment, we could also
scratch it, because that would mean that nobody has ever used it.

Do you think it has merit? Which would that be? And would you be willing
to derive tests for the test suite?

I have never written a test suite for anything (I only write perl for personal small utility scripts), so I wouldn't know where to begin.

However, given that literally a one-word fix will patch this up and restore the functionality advertised by the man page, wouldn't that be the wiser course of action?

After all, scratching the environment variable completely would require modifying documentation, leaving a number of related but different online sources out of date (e.g. here ), and so on. All of that seems more drastic than simply switching that push to an unshift.

briandfoy added a commit to briandfoy/cpanpm that referenced this issue Dec 7, 2022
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