Why can't I run a Perl program from TextMate? - perl

I'm following a bioinformatics text, and this represents one of my first Perl scripts. While in TextMate, this does not produce any result. Is it functioning? I added "hello world" at the bottom and I don't see that when I run the script in TextMate. What have I done wrong?
#!/usr/local/bin/perl -w
use lib "/Users/fogonthedowns/myperllib";
use LWP::Simple;
use strict;
#Set base URL for all eutils
my $utils = "http://eutils.ncbi.nlm.nih.gov/entrez/eutils";
my $db = "Pubmed";
my $query ="Cancer+Prostate";
my $retmax = 10;
my $esearch = "$utils/esearch.fcgi?" .
"db=$db&retmax=$retmax&term=";
my $esearch_result = get($esearch.$query);
print "ESEARCH RESULT: $esearch_result\n";
print "Using Query: \n$esearch$query\n";
print "hello world\n";

Have you run other scripts successfully in TextMate? I suspect that you have an editor configuration issue (or that URL is not hittable and the script is generating an error).
Try running an even simpler script and see what happens, e.g. something like:
#!/usr/local/bin/perl -w
print "hello world\n";
Next, see if you can see error output:
#!/usr/local/bin/perl -w
warn "oh noes";

Most likely, your executing environment is not set up in the right way, i.e., TextMate does not know how to execute a Perl script and display the results to you. Have you tried running it from the shell?

Do you have /usr/local/bin/perl installed? What does which perl return at the terminal?
If your hashbang line is incorrect then when you come to run the script in TextMate you will just get an empty output window. Textmate does not give any error that it couldn't find the executable interpreter you prescribed after #! :(
Replace the first line with #!/usr/bin/env perl and it will run you login's default Perl (found in $PATH environment variable).
/I3az/

Related

Printing the result of the script(running in background) on the terminal

I am writing a perl script which runs another tcl script from it. The terminal doesn't print anything and waits for the tcl script to complete.
`chmod +x runme.tcl`; `./runme.tcl 2>&1`;
Can anyone please help me on how to print the results of the tcl script on the terminal instead of just waiting for it to get completed?
Thank you
system('chmod +x runme.tcl');
system('/runme.tcl 2>&1');
You can run tcl scripts directly from perl using the Tcl module without having to mess around with qx or system:
#!/usr/bin/env perl
use warnings;
use strict;
use Tcl;
Tcl->new->EvalFile("runme.tcl");
It'll share the same standard output as the perl script.
If you're using a new enough version of Tcl, you can easily create a safe interpreter to evaluate the script in case it tries to do anything nasty:
#!/usr/bin/env perl
use warnings;
use strict;
use Tcl v1.05;
my $interp = Tcl->new;
my $safeinterp = $interp->CreateSlave("safeinterp", 1);
$interp->Eval('interp share {} stdout safeinterp');
$interp->Eval('interp share {} stderr safeinterp');
$safeinterp->EvalFile("runme.tcl");
Backticks capture the output of an external command. You can write that output with a print command in front of the backticks.
`chmod +x runme.tcl`; print `./runme.tcl 2>&1`;

Use of "-w" in the shebang line

I am new to perl scripting and I am wondering what is the use of "-w" in hashbang of perl scripts.
#!/usr/bin/perl -w
my $userid = $ENV{USER};
print "$userid\n";
I know that this part #!/usr/bin/perl is used to inform shell that it is a perl script so that no need to use perl before running the script.
But I couldn't find the exact meaning for -w and most of the scripts I saw have this option .
Kindly let me know if that has any significance as I could not find any difference.
Edit:
Below document is very helpful(Suggested by #Toto)
https://perldoc.perl.org/5.32.0/perlrun.html
Everything after the command is passed as arguments to the command being invoked.
The -w option to the perl command is to enable warnings.

Unable to parse command line long options

#!/usr/bin/perl -sw
use strict;
use warnings;
use Getopt::Long;
my $remote = 0;
my $test = 0;
GetOptions ('remote' => \$remote, 'test' => \$test);
print "$remote:$test\n";
perl test.pl --remote --test
The above prints "0:0". I am new to Perl so I have been unable to determine why this isn't working.
I also ran the "Simple Options" section from http://perldoc.perl.org/Getopt/Long.html#Simple-options and that didn't produce anything either.
I believe the -s command line option you include on your she-bang line is biting you. According to the perlrun documentation, the -s command line option:
enables rudimentary switch parsing for switches on the command line after the program name but before any filename arguments (or before an argument of --).
If you remove that option, things should work as you expect. I would also recommend removing the -w since you are already using the use warnings directive (the use warnings directive is much more fully featured, essentially replacing the -w option).
So, long story short, make your first line:
#!/usr/bin/perl
Note that if running the script on Windows via cmd you must specify perl before the script name otherwise GetOptions doesn't work.
When I tried simply calling my script.pl on the command line without first putting perl the script ran but all the options weren't parsed.

Get the current working directory path in Perl

I am getting the perl script location using $FindBin::RealBin. Now I have a problem using this.
I am calling a Perl script from one Perl script.
In the caller script, $FindBin::RealBin is working fine, but in the called Perl script, it is not giving the location.
Am I missing anything?
This is what I always use:
my ($vol,$script_path, $prog) = File::Spec->splitpath(File::Spec->rel2abs( __FILE__ ));
Check if it works in your case. It should work if you call your inner script as a shell call. I don't know if it would work if you call it with do.
Some readings about this:
see How do I get the full path to a Perl script that is executing?
FindBin::Bin is broken http://use.perl.org/~Aristotle/journal/33995 (or the google cache http://webcache.googleusercontent.com/search?q=cache:y-5OZsxdTT8J:use.perl.org/~Aristotle/journal/33995)
File::Basename http://perldoc.perl.org/File/Basename.html is more problematic
Hope it helps
As you did not provide a full code sample, this is more a guess.
According to the documentation, you need to call
FindBin::again();
as this is a known limitation of FindBin.
If I understand your question, you can use realpath from Cwd .
$ cat ./mycode
#!/usr/bin/env perl
use strict;
use warnings;
use Cwd;
print "called as '$0'\n";
print "lives in '", Cwd::realpath($0), "'\n";
$ ./mycode
called as './mycode'
lives in '/Users/jrf/Sandbox/mycode'

How can you get the metadata of an image by the Perl module?

I installed the following component by MacPorts:
p5-image-info #1.16 (perl, graphics)
Extract meta information from image files
It says in its website that you can use it by
Usage is something like this:
use Image::Info qw(image_info);
#info = image_info("filename");
$refto_hash_describing_1st_image = $info[0];
$refto_hash_describing_2nd_image = $info[1];
However, I run unsuccessfully
$perl use Image::Info qw(image_info);
-bash: syntax error near unexpected token `('
$
How can you get the metadata of an image by the Perl module?
The syntax described is how you would use it within a Perl script, not how you can use it as a single line from the shell.
Put this in a .pl file (e.g. "image_info.pl"):
#!/usr/bin/perl -w
use strict;
use Image::Info qw[image_info];
use Data::Dumper;
while (#ARGV) {
print Dumper(image_info(shift));
}
And run it thus:
$ ./image_info.pl file.jpg
and revel in the masses of information it will tell you...
Read
http://perldoc.perl.org/perlrun.html