Bootstrap FreeKB - Perl (Scripting) - ( and ) characters
Perl (Scripting) - ( and ) characters

Updated:   |  Perl (Scripting) articles

Sometimes, the ( and ) characters can be problematic. First let's consider the following markup. This will print the text Hello(World) without issue. In other words, the ( and ) characters do not create any problems.

my $foo = "Hello(World)";
print "$foo\n";

 

On the other hand, let's say you are making an SSH connection to another server and checking if a file that contains the ( and ) characters exists.

#!/usr/bin/perl

use strict;
use warnings;

my ($stdout) = `ssh johndoe\@example.com ls "/path/to/example(file).txt";

 

This will produce the following standard error.

sh: -c: line 0: syntax error near unexpected token `('

 

The easiest fix is to place a single quote or double quote around the command being issued, like this:

my ($stdout) = `ssh johndoe\@example.com 'ls "/path/to/example(file).txt"';

 

A more convoluted fix would be to escape the ( and ) characters. However, this is certainly not ideal, as there may be other special characters that would also need to be escaped, so if you are going to use this method, you are going to eventually end up with a long list of special characters being escaped.

if ( $foo =~ '\(' or $foo =~ '\)' ) {
  $foo =~ s|\(|\\(|g;
  $foo =~ s|\)|\\)|g; 
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter f10d95 in the box below so that we can be sure you are a human.