No run time & compile time error in Perl - perl

I am trying to execute the following small script.
test.pl
#!/bin/perl
use strict;
$value = 1;
The output is
perl -c test.pl
test.pl syntax OK
Please let me know why it is not throwing error?
Thanks in advance.

What version of Perl are you using? This is Perl 5.18:
$ cat test1.pl
#!/bin/perl
use strict;
$value = 1;
$ perl -c test1.pl
Global symbol "$value" requires explicit package name at test1.pl line 3.
test1.pl had compilation errors.
I get the error.
Is it possible you're not running against the Perl script you think you are?

Related

How to call a Perl program from a csh script

I have this simple code to execute, but have failed.
a.csh
#! /bin/csh -f
`perl a.pl`
a.pl
#use perl
eval 'exec perl -w -S $0 ${1+"$#"}'
if 0;
use Cwd;
print "here.\n";
When I run a.csh, it reports an error as below.
here: Command not found
I am not sure what that means; any suggestion is welcome.
The backticks in your program are returning the output of the perl script. Then csh tries to execute that output as csh. toolic's comment telling you to remove the backticks is correct: perl a.pl. Reading more about how backticks work might be useful.

Access perl module within Bash script

I have a perl module with data definitions (hashes, arrays, etc.), is there any way I can access that data from inside a bash script? This isn't working for me...
#!/bin/bash
perl -e 'use Data'
tests=`perl -e "#tests"; `
echo "Perl tests = ${tests}" # prints "Perl tests = "
The module looks something like this:
our #EXPORT_OK = qw( #tests );
our #tests = qw( 1 2 3 4 5 );
If you have package variable #tests inside Data module,
perl -MData -e 'print "$_\n" for #Data::tests'
For perl 5.10 and above,
perl -MData -E 'say for #Data::tests'
You can use a module from the command line with -M
perl -MData -e'print map {"$_\n"} #tests;'
In the code you give, you run one interpreter that loads Data. It exits. You then run a second interpreter, which prints #tests. As that's the only action the second interpreter has performed, it's uninitialized, and prints nothing.

Running Perl scripts from a file in linux

I'm in Linux (CentOS) trying to run a basic Perl script from a file. I am getting an error. I think it has something to do with the syntax, but I can't figure it out.
$ perl -e 'print "Hello World\n";'
This runs fine and will print Hello World on the next line. However, when I put this into vi and save it as perlOne, exactly the same (perl -e 'print "Hello World\n";') and run the command:
$ perl perlOne
I get the error: "syntax error at perlOne line 1, near "perl -e -- Execution of perlOne aborted due to compilation errors."
It's the same line, but it does not work in the file.
I'm working through the Perl tutorial from Linux Pro Magazine - which is where I got this from.
The command-line switch -e allows you to run code from the command line, instead of having to write your program to a file and then execute it.
$ perl -e 'print "Hello World\n";'
Output:
Hello World
If you want to run it from your file you will need to write it differently:
#!/usr/bin/perl
use strict;
use warnings;
my $message = 'Hello World';
print $message . "\n";
# You can also make it directly
print "Hello World\n";
While I do not recommend it can also be written as:
print "Hello World\n";

Pass parameter to a perl script executed trough qsub

Hi would like to pass a parameter to my perl script that should be executed trough qsub.
So I run:
qsub -l nodes=node01 -v "i=500" Test.pl
While in Test.pl I try to call i parameter in several way:
use Getopt::Long;
$result = GetOptions ("i" => \$num);
open(FILE,">/data/home/FILEout.txt");
print FILE "$num\n";
print FILE "$ARGV[0]";
close(FILE);
Unfortunatelly output file of the perl script is always empty.
Do you have any suggestions? Where I'm wrong? Help please
According to all documentation I can find -v sets an environment var, so you'd use $ENV{i} to get 500. (Check your own documentation to confirm.)
If you wanted to actually pass an arg to your script, you could try using
qsub ... Test.pl -i=500
But based on my web search, that might only work for some versions of qsub. Others would require that you make a helper script (say Test.sh)
#!/bin/sh
Test.pl "-i=$i"
along with the command
qsub ... -v 'i=500' Test.sh
If qsub ... Test.pl ...args... is supported and you can change your script, the simplest solution is
qsub ... Test.pl 500
and
my ($i) = #ARGV;
I Finally get the solution that works with PBRProfessional 10.4.
There are two way to solve it:
First one is the following
echo "perl /path/to/Test.pl -i 500" | qsub -l nodes=node06
Second one is two use
qsub -l nodes=node06 -v i=500 Test.pl
and read the parameter in the Test.pl through $ENV{i}

calling perl subroutine in shell script

i made a Perl Module MyModule.pm it has some subroutines getText which I wanted to call in a shell script.
i tried following manner but it gives error;
SEC_DIR=`perl -MMyModule -e 'getText'`; # line 1
echo $SEC_DIR
exit 0
Returned Error;
Can't locate MyModule.pm in #INC (#INC contains: /usr/lib/perl5/5.10.0/x86_64-linux-thread-multi /usr/lib/perl5/5.10.0 /usr/lib/perl5/site_perl/5.10.0/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.10.0 /usr/lib/perl5/vendor_perl/5.10.0/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.10.0 /usr/lib/perl5/vendor_perl .).
BEGIN failed--compilation aborted.
PS: .pm file and .sh are at same location.
some other options I tried;
line 1:
SEC_DIR=`perl -MMyModule -e '&getText'`;
SEC_DIR=`perl -MMyModule -e 'use MyModule; getText'`;
SEC_DIR=`perl -e 'use MyModule; getText'`;
It's hard to say what went wrong not knowing what's in MyModule.pm.
#INC looks ok (. is in the list, so there should be no problem with locating MyModule.pm in current directory).
Here's a minimal example that works the way you described. Hope it helps.
$ cat SomeModule.pm
package SomeModule;
sub testsub
{
return "it works\n";
}
1;
$ VAL=`perl -I. -MSomeModule -e 'print SomeModule::testsub'`
$ echo $VAL
it works
Another way to load the module would be:
$ perl -e 'require "./SomeModule.pm"; print SomeModule::testsub()'
it works