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

lib/B/Deparse.t: many "Variable not imported"-related failures #194

Open
jkeenan opened this issue Aug 13, 2020 · 2 comments
Open

lib/B/Deparse.t: many "Variable not imported"-related failures #194

jkeenan opened this issue Aug 13, 2020 · 2 comments
Labels
bug Something isn't working

Comments

@jkeenan
Copy link
Collaborator

jkeenan commented Aug 13, 2020

Consider this program, which will be run from the root directory of a git checkout of the Perl core distribution. The program is adapted from lib/B/Deparse.t.

# $ cat ~/tmp/deparse-problem.pl 
my @INC = ("./lib");
my $path = join " ", map { qq["-I$_"] } @INC;
my $alpha = qq|$^X $path "-MO=Deparse" -anl -e 1 2>&1|;
print "XXX: $alpha\n";

my $output = `$alpha`;
print "YYY: $output\n";

We first run it from Perl 5.

$ ./perl -Ilib ~/tmp/deparse-problem.pl 
XXX: /home/jkeenan/gitwork/perl/perl "-I./lib" "-MO=Deparse" -anl -e 1 2>&1
YYY: -e syntax OK
BEGIN { $/ = "\n"; $\ = "\n"; }
LINE: while (defined($_ = readline ARGV)) {
    chomp $_;
    our @F = split(' ', $_, 0);
    '???';
}

We run it again from Perl 5, but this time imposing strictures.

$ ./perl -Ilib -Mstrict ~/tmp/deparse-problem.pl 
XXX: /home/jkeenan/gitwork/perl/perl "-I./lib" "-MO=Deparse" -anl -e 1 2>&1
YYY: -e syntax OK
BEGIN { $/ = "\n"; $\ = "\n"; }
LINE: while (defined($_ = readline ARGV)) {
    chomp $_;
    our @F = split(' ', $_, 0);
    '???';
}

No change in output. Next, we run it against a Perl 7 executable where we have strict-by-default.

$ ./perl -Ilib ~/tmp/deparse-problem.pl 
XXX: /home/jkeenan/gitwork/perl-atoomic-2/perl "-I./lib" "-MO=Deparse" -anl -e 1 2>&1
YYY: Variable "$savebackslash" is not imported at (eval 1) line 25.
Loading compiler backend 'B::Deparse' failed: Global symbol "$savebackslash" requires explicit package name (did you forget to declare "my $savebackslash"?) at (eval 1) line 25.
 at -e line 0.
BEGIN failed--compilation aborted.

It fails. Now, as I've been working my way through hundreds of test files in the core distribution, "Variable $___ is not imported" failures can usually be resolved by fully qualifying the variable -- something like $main::savebackslash -- or making the variable a package-scoped global variable -- our $savebackslash.

However, I have had no luck with that approach in this point. That's probably because, in lib/B/Deparse.pm, this variable already is fully qualified.

 851 sub compile {
 852     my(@args) = @_;
...
 862     if ($/ ne "\n" or defined $O::savebackslash) { # deparse -l and -0
 863         my $fs = perlstring($/) || 'undef';
 864         my $bs = perlstring($O::savebackslash) || 'undef';
 865         print qq(BEGIN { \$/ = $fs; \$\\ = $bs; }\n);
 866     }

There are many unit tests within lib/B/Deparse.t which fail with this kind of problem. I don't understand B::Deparse well enough to know how to fix them. @atoomic

Thank you very much.
Jim Keenan

@atoomic
Copy link
Owner

atoomic commented Aug 13, 2020

I think the error comes from O.pm and the patch should be something like this

diff --git a/ext/B/O.pm b/ext/B/O.pm
index 81c879835b..1388f21822 100644
--- a/ext/B/O.pm
+++ b/ext/B/O.pm
@@ -27,7 +27,8 @@ sub import {
            B::save_BEGINs;
        }

+    our $savebackslash;
      CHECK {
            if ($quiet) {
                close STDOUT;
                open (STDOUT, ">&", $saveout_fh);

note: when running ./perl -Ilib -Mstrict ~/tmp/deparse-problem.pl you are not enforcing strict to the perl command you are running later in the script

@jkeenan
Copy link
Collaborator Author

jkeenan commented Aug 13, 2020 via email

@jkeenan jkeenan added the bug Something isn't working label Aug 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants