-1

I have the following Perl. It attempts to detect a Git Clone (as opposed to an offline zip) and print the repo information. The idea is, if git rev-parse fails, then its probably not a Git repo.

if (system "git rev-parse HEAD") {                         # Line 234
    my $revision = `git rev-parse HEAD | cut 1-16`;        # Line 235
    my $branch = `git rev-parse --abbrev-ref HEAD`;        # Line 236
    print "Git repo: $branch ($revision)\n";               # Line 237
}                                                          # Line 238

It results in the errors below. If I am parsing What's the differences between system and backticks and pipes in Perl and How can I store the result of a system command in a Perl variable correctly, it should work.

According to How to print variables in Perl, print "Git repo: $branch ($revision)\n" should work. Based on Global symbol requires explicit package, the Q&A says to use @ instead of $. I tried to print "Git repo: @branch (@revision)\n", but it resulted in the same errors (with a different symbol).

I have two questions:

  • Why does Perl think the variable is a package?

  • What is wrong with the Perl, and how do I fix it?


Global symbol "@branch" requires explicit package name at ./Configure line 237.
Global symbol "@revision" requires explicit package name at ./Configure line 237.
Community
  • 1
  • 1
jww
  • 97,681
  • 90
  • 411
  • 885
  • The `system` returns `0` on success. So if you get inside your `if` block it means that the command (itself) `git rev-parse HEAD` failed. Then the same will happen for it in `qx` (backticks). I suspect that this isn't what you have in mind? As for the error you are getting, I have no idea. It means that the variable `$branch` isn't declared which is weird. – zdim Jun 29 '16 at 07:12
  • Can you reduce your code to a short, runnable program which demonstrates the problem? Using the code posted in the question (with the logic in the `if` reversed, for reasons @zdim already explained), I am unable to reproduce the errors you describe. Aside from the text being formatted poorly (linefeeds at the end of `$branch` and `$revision`), it works perfectly for me. – Dave Sherohman Jun 29 '16 at 07:34
  • @zdim - thanks. I have not gotten that far because of the Global Symbol errors (but I suspected it). – jww Jun 29 '16 at 07:35
  • I sincerely doubt that code is giving those errors. The errors are complaining that you used two unknown array variables `@branch` and `@revision`, where no such array variables are in your posted code. More than likely, your actual code that generated those errors incorrectly has `@branch` and `@revision` (or perhaps something like `$branch[0]` and `$revision[0]`) rather than `$branch` and `$revision` on line 237, and you fixed it when you retyped it here, but not in your original source. – Paul L Jun 29 '16 at 19:17

1 Answers1

0

I can't test your script, but this part should be like this:

if(`git rev-parse HEAD 2>&1` !~ /Not a git repository/) {
    chomp(my $revision = `git rev-parse HEAD | cut -c1-16`); 
    chomp(my $branch = `git rev-parse --abbrev-ref HEAD`);
    print "Git repo: $branch ($revision)\n";
}