Error de sintaxis de Perl: se encontró una palabra desnuda donde el operador esperaba
Frecuentes
Visto 4,638 equipos
0
This is my perl script code. in this i'm getting error like "bareword found where operator expected at $cmd"
my $path = $folder."/".$host."_".$database_name.".bak";
$cmd .= "-U $user_name -P $password -S $host -d master -Q "BACKUP DATABASE [$database_name] TO DISK = N'$path'" ";
alguien me ayude?
2 Respuestas
4
When a string has double quotes within it, you need to escape them with \
.
$cmd .= "-U $user_name -P $password -S $host -d master -Q \"BACKUP DATABASE [$database_name] TO DISK = N'$path'\" ";
Also, Perl lets you use other characters for quote delimiters. qq
followed by almost any character is the same as double quotes. So you could do things like this to avoid the need of backslashes:
$cmd .= qq(-U $user_name -P $password -S $host -d master -Q "BACKUP DATABASE [$database_name] TO DISK = N'$path'" );
$cmd .= qq|-U $user_name -P $password -S $host -d master -Q "BACKUP DATABASE [$database_name] TO DISK = N'$path'" |;
Y así sucesivamente ...
Actualizar: How to execute a system command in Perl. There are three basic ways:
system($cmd); #Goes through the shell if shell metacharacters are detected.
system(@command_and_args); #first element is the command, the rest are arguments
system
executes a command and waits for it to return. The return value is the exit status of the program.
my @results = `$cmd`; #Always goes through shell.
tildes execute a command and return its output. You should only use this if you actually need the output; otherwise, it is better to go with system
.
exec $cmd;
exec @command_and_args;
exec
is exactly like system, except that it never returns. It effectively ends your program by calling another program.
Use the one that is most appropriate to your situation. Or in this case, since you are executing SQL, consider using the Dbi module. It's definitely a better approach for anything more than a couple of simple commands.
Respondido 22 Oct 12, 09:10
1
Parece que tienes tu "
characters in the wrong place. I'm not sure where they should be.
In your second line, the string literal:
"-U $user_name -P $password -S $host -d master -Q "
is immediately followed by the bareword
BACKUP
Respondido 22 Oct 12, 08:10
I changed the code like this $cmd .= "-U $user_name -P $password -S $host -d master -Q "."BACKUP DATABASE [$database_name] TO DISK = N'$path'" ; - Stefan Edwards
@StefanEdwards: Doubtful. I copy-and-pasted the line of code from your comment, and it compiles without error. - Keith Thompson
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas perl sql-server-2008 config or haz tu propia pregunta.
I have another one doubt. What is the command is used to run this sql query from the perl script - Stefan Edwards
@StefanEdwards, I updated the answer with info on running system commands. If you are having trouble, the first thing to look at in my experience is quoting. This can be finicky, especially if you are on Windows. I know some other question on SO have addressed this. - dan1111