BEGIN: command not found in Perl - perl

I have a Perl program but when I am trying to run it, it's throwing the error
*****BEGIN: command not found
+ our =/prod/home01/appl/psoft/epmapp/batch/lib
/prod/home01/appl/psoft/epmapp/batch/bin/FileTransfer.pl: line 4: our: command not found
+ push #INC,
/prod/home01/appl/psoft/epmapp/batch/bin/FileTransfer.pl: line 5: push: command not found
/prod/home01/appl/psoft/epmapp/batch/bin/FileTransfer.pl: line 6: syntax error near unexpected token `}'
/prod/home01/appl/psoft/epmapp/batch/bin/FileTransfer.pl: line 6: `}'*****
I think the error is due to the fact that its trying to run the script via the shell instead of via perl. I have checked all the basic things, like there is no extra line before the she bang in my script. But still i am not able to figure out what the exact prob is.
Here is the start of our code:
#!/usr/bin/perl
BEGIN {
our $lib_path="/prod/home01/appl/psoft/epmapp/batch/lib";
push #INC, $lib_path;
}
I am using Red Hat Enterprise Linux Server release 5.9 environment.

Clearly it is interpreted as bash script. If shebang line is exactly as you pasted it, then it must be something wrong with perl binary. Don't you try to run it invalid way e.g. with "bash test.pl", or "source test.pl" from shell? Anyway the code is syntactically correct.
dtpwmbp:~ pwadas$ cat test.pl
#!/usr/bin/perl
BEGIN {
our $lib_path="/prod/home01/appl/psoft/epmapp/batch/lib";
push #INC, $lib_path;
print $lib_path . "\n";
}
dtpwmbp:~ pwadas$ bash test.pl
test.pl: line 3: BEGIN: command not found
test.pl: line 4: our: command not found
test.pl: line 5: push: command not found
test.pl: line 6: syntax error near unexpected token `}'
test.pl: line 6: `}'
dtpwmbp:~ pwadas$ ./test.pl
/prod/home01/appl/psoft/epmapp/batch/lib
dtpwmbp:~ pwadas$

If you can't stop the perl script being executed as a shell script, then adding the magic lines
eval 'exec /usr/bin/perl -S $0 ${1+"$#"}'
if 0; # not running under some shell
below the shebang might help.

Related

Perl command executing good when run on command line but not working in the Perl script

Below is the code I'm trying to execute. I have mentioned the line 266 in the code. I have added that code to remove the blank lines in the log file. I'm not sure whether we can run the perl command inside a Perl script. Is there another way that I can run this so that I can remove the blank lines in the log file?
Below is the error I'm getting while running through the Perl script:
syntax error at ./reportJBossErrors.pl line 266, near "n -e "
Execution of ./reportJBossErrors.pl aborted due to compilation errors.
Here is a portion of the code, showing line 266:
sub main {
readConfiguration($config_file);
$short_hostname = `hostname | cut -f 1 -d.`;
chomp $short_hostname;
getFileandInstance($short_hostname);
$yesterday = getYesterday();
validateEnvironment();
$log_file = getLogFile($FMASK,$yesterday);
perl -i -n -e "print if /\S/" $log_file; # 266 line. This is where I'm getting the compilation error
processFile($log_file);
$html_out = writeEmail();
sendEmail($CONFIG{"FROMADDR"},$CONFIG{"TOADDR"},"Normal",
"JBOSS",$short_hostname,$log_file,$CONFIG{ENVTYPE},$html_out);
}
You can not call the perl command inside a Perl program as if it were a Perl builtin function. You can use system to run an external command:
my $cmd = 'perl -i -n -e "print if /\S/"';
system "$cmd $log_file";
You need to be careful of quoting. Since you have a file name/path in the Perl variable $logfile, which you want to interpolate, that can go inside double quotes. Since you do not want to interpolate \S, that should go in single quotes.
You cannot invoke the perl executable inside a Perl program as if it were a Perl builtin function. Instead, use the list form of system to run an external command. Don't forget to check if the command succeeded:
my #cmd = (perl => '-i', '-n', '-e', 'print if /\S/', $log_file);
system(#cmd) == 0
or die "system #cmd failed: $?";
In general, I would recommend using the full path to perl rather than relying on $PATH.
Also, if you need to keep track of status etc, use Capture::Tiny to get both STDOUT and STDERR of the command you are running so that you can log error information.

Re: https://stackoverflow.com/questions/23937389/determine-parent-shell-from-perl/25139489

Has anyone tried this code on cygwin?
I get these errors:
Can't find string terminator "'" anywhere before EOF at -e line 1.
Use of uninitialized value in pattern match (m//) at ./dos_it.pl line 506.
Use of uninitialized value $shellpath in rindex at ./dos_it.pl line 586.
Use of uninitialized value $shellpath in substr at ./dos_it.pl line 586.
Use of uninitialized value $pathToShell in concatenation (.) or string at ./dos_it.pl line 761.
Use of uninitialized value $shell_conformance in concatenation (.) or string at ./dos_it.pl line 761.
The string that is generated is:
$ ps -ef | perl -ane '1..1 and /^(.*)CO?MM?A?N?D/ and $s=length $1;s/^.{$s}//; print "#F[1,2] $_"'
Perl version:
$ perl -v
This is perl 5, version 26, subversion 2 (v5.26.2) built for MSWin32-x64-multi-thread
That is a command for the Bourne shell (sh) or similar, but you gave it to the Windows shell (cmd) to execute. Execute the command using sh or similar (whether cygwin-built or otherwise) to get rid of the error.
By the way, you were using a Windows build of Perl (MSWin32-x64 arch), not a cygwin build of Perl (cygwin arch). That's not the cause of the error, as the program will run fine either way. That said, this "issue" will surely go away once you use a cygwin-built sh or similar to execute the command.

syntax error in perl script for linux

I have written a very simple perl script for Linux to determine the current user logged on.
However I keep getting the following error when trying to run it:
bash: use: command not found
bash: my: command not found
bash: ./test.pl: line 9: syntax error near unexpected token `else'
bash: ./test.pl: line 9: `} else {'
This is my code:
#!/usr/bin/perl
use strict;
my $loginName = '';
if ($^O =~ /MSWin/i)
{
$loginName = getlogin;
} else {
#else it is unix
$loginName = getpwuid($<);
}
print $loginName;
I have tried to google this but I dont see what I am doing wrong with my if statement? It works fine on Windows.
Thank you
You are invoking the script incorrectly: these errors are clearly from bash, while perl should be running the script instead.
I don't know how you're running it now, but (assuming its filename is mywhoami) you can always invoke perl explicitly:
perl mywhoami
It should also work to make it executable
chmod a+x mywhoami
and then execute it:
./mywhoami
I think it is something it the way you run the script.
Please try to run it as follow:
$perl <script.pl>

Perl, Command not found after entering "perl -v"

I'm new to Perl.
Running Fedora 16
I have installed Perl recently, after the installation, I checked via "perl -v",
I got the following lines:
/usr/bin/perl: line 19: use: command not found
/usr/bin/perl: line 20: use: command not found
/usr/bin/perl: line 23: my: command not found
/usr/bin/perl: line 24: my: command not found
/usr/bin/perl: line 25: my: command not found
/usr/bin/perl: line 28: syntax error near unexpected token `('
/usr/bin/perl: line 28: `my %list=();'
Can I know why is it so?
Thank you.
That output is the output of a Perl script being executed by a bourne shell instead of perl.
perl is very funky on your setup. Maybe it has been accidentally replaced? Maybe you have an alias named perl? (What does type perl output?)
I had exactly the same problem.
Finally it was some so silly as that I had a blank space before the first line: #!/usr/bin/perl
Then, that line was ignored and the script was being interpreted by bash insted by perl.
I hope it helps to someone.
I'd be very surprised if there is a unix-like system which ships without perl. I'm not surprised that there are problems if more than one version are installed over each other.
If you have to use that system and can't start over, try installing it in some other location like /opt
Does #!/usr/bin/perl exists in your script at the begining?
I think its trying to run the script through the shell instead of perl, Check whether the script is using the encoding character UTF8 and finally try to invoke perl explicitly using the command perl filename.
Try to login with ROOT and try the same
Try the below command
which perl
You should get output as /usr/bin/perl

Why won't this example from 'Learning Perl 6th Edition' run?

I am stuck on chapter 2 exercise 2 page 42 of Learning Perl 6th Edition.
I copied the code example for the problem from page 296. I am using Perl version 5.10.1 on Ubuntu 11.04. I get errors that I cannot figure out could someone please help? I will list the code and the error message below.
#!/usr/bin/perl -w
$pi = 3.141592654;
print "What is the radius? ";
chomp($radius = <STDIN>);
$circ = 2 * $pi * $radius;
print "The circumference of a circle of radius $radius is $circ.\n";
The error I get is:
./ex2-2: line 3: =: command not found
Warning: unknown mime-type for "What is the radius? " -- using "application/octet-stream"
Error: no such file "What is the radius? "
./ex2-2: line 5: syntax error near unexpected token `$radius'
./ex2-2: line 5: `chomp($radius = <STDIN>);'
You are executing the Perl script using your shell instead of perl. Based on the fact that the line numbers are off by one, I suspect the cause of the problem is a blank line before the shebang (#!) line. #! must be the first two bytes of the file. Delete this blank line.
If that's not the problem, then perhaps you executed your script using
. ex2-2
or
sh ex2-2
when you should have used
perl ex2-2
or
ex2-2 # if "." is in your $PATH
or
./ex2-2
The last two requires that you make the script executable (chmod u+x ex2-2).
It would help if you copied and pasted exactly what you executed. Notice that the line numbers are different in the example below:
$ cat x.pl
#!/usr/bin/perl -w
$pi = 3.141592654;
print "What is the radius? ";
chomp($radius = <STDIN>);
$circ = 2 * $pi * $radius;
print "The circumference of a circle of radius $radius is $circ.\n";
$ sh x.pl
x.pl: line 2: =: command not found
x.pl: line 3: print: command not found
x.pl: line 4: syntax error near unexpected token `$radius'
x.pl: line 4: `chomp($radius = <STDIN>);'
$
This was with Bash 3.x on MacOS X 10.7.1.
Given that output, I can confidently diagnose that your script was run as a shell script and not as a Perl script; bash was used to run it.