1

I am trying to execute the following gpg command from within Perl:

`gpg --yes -e -r me@mydomain.com "$backupPath\\$backupname"`;

However I get the following error:

Global symbol "@mydomain" requires explicit package name (did you forget to declare "my @mydomain"?)

Obviously I need to escape the '@' symbol but don't know how. How do I execute this command in Perl?

toolic
  • 57,801
  • 17
  • 75
  • 117
user5013
  • 981
  • 4
  • 10
  • 21
  • 2
    Escape the `@` with a backslash? `... -r me\@mydomain.com ...` – Robert Sep 03 '20 at 16:05
  • Perhaps find a Perl module instead of using a shell command. I found for example [GnuPG](https://metacpan.org/pod/GnuPG) – TLP Sep 03 '20 at 19:47

2 Answers2

3

When you do:

`gpg --yes -e -r me@mydomain.com "$backupPath\\$backupname"`;

perl sees the @mydomain part and assumes you want to interpolate the @mydomain array right into the string.

But since there was no @domain array declared, it gives you the error.

The fix is simple: To tell perl that you want to treat @mydomain as a string and not as an array, simply put a backslash (\) before the @, like this:

`gpg --yes -e -r me\@mydomain.com "$backupPath\\$backupname"`;
J-L
  • 1,786
  • 10
  • 13
-1

Backticks run process in sub shell (slower, consumes more resources) and have some issues which you should investigate.

Following code demonstrates other approach which does not spawn sub shell.

use strict;
use warnings;
use feature 'say';

my $backupPath = '/some/path/dir';
my $backupname = 'backup_name';

my $command = "gpg --yes -e -r me\@mydomain.com $backupPath/$backupname";
my @to_run = split ' ', $command;

system(@to_run);
Polar Bear
  • 6,762
  • 1
  • 5
  • 12