How to check, is excel plugin enabled in PERL - perl

Am just started working for exporting excel spread sheet in PERL,
Before start i want confirmation, that is, is excel spread sheet realted plug is enabled or not,
Advise some simple PERL syntax ,
Thanks

If you are using a module from CPAN such as Excel::Writer::XLSX then you can just use a simple script to test if the module is installed:
#!/usr/bin/perl
use Excel::Writer::XLSX;
And run that script. You'll get an error about
Can't locate Excel/Writer/XLSX.pm in #INC
if the module is not installed on your system.

If by "excel plugin" you mean a CPAN module, then the solution is to try to load it and see what happens. If you wrap the loading attempt in an "eval" statement then your program won't die if it fails.
Something like this:
eval 'use Spreadsheet::ParseExcel';
my $have_module = ! $#;
This method is a little imprecise as I'm just looking to see if $# has been given a value. For more control, you might want to check the contents of $# for specific error message. You're looking for one that starts "Can't locate ...".

Assuming you mean:
How can I tell if an Excel document delivered from my webserver to a browser will open in a browser plugin (as opposed to being saved or opened in a standalone application)?
Then you can't. Browsers don't send that information to the server, so no server side process (written in Perl or otherwise) can tell.

perl -MExcel::Writer::XLSX -e "print 'ok'"
If it prints 'ok' then you're good. If you get a can't locate ... in #INC then you need to install it.

Related

how to make a perl script create a directory and a makefile in the script

I was going through lessons and i saw a practise saying to make a script that creates a directory and a makefile inside the actual script. i know how to do this in the ubuntu terminal with mkdir but can you actually create in a script? any help on this please?
Instead of coming straight to Stack Overflow for help, it would have been better to have started with some research of your own. I suggest the perldoc utility that is installed with Perl. The same information is also available on line at perldoc.perl.org
It sounds like you need—surprise!—the mkdir operator, or you could look at the core File::Path module
As for the make file, you don't say anything about what you need, but a simple open, like this will do the trick
open my $fh, '>', '/path/to/makefile' or die $!
and then you can just print what you like to it

"use strict;" line in perl causing a simple print script to fail to run

I installed apache and perl a few days ago and have been successful in running a few scripts, but I have not been able to get a single script to run after putting the "use strict;" line in. All I see upon adding that line is a very generic "Internal Server Error" with ZERO unique information.
Here's a script that does gives the error:
#!/usr/bin/perl
use strict;
print "Content-Type: text/html", "\n\n";
print "Hello World";
Cannot find anyone else having this problem, really puzzling me. Could it be some setting in my installation of perl or something?
Always check the error log in situations like this. Let it tell you what's wrong.
There are at least three likely possibilities:
The script is not executable, and so will not run. (unix specific)
#!/usr/bin/perl does not exist and so can't be executed.
Your #INC is messed up some how, and so strict cannot be found.
Your error log should be able to say if it is one of these fairly quickly.

How to specify in the script to only use a specific version of perl?

I have a perl script written for version 5.6.1 and which has dependencies on Oracle packages, DBI packages and more stuff. Everything is correctly installed and works.
Recently perl version 5.8.4 got installed and because those dependencies are not correctly set so the script fails.
'perl' command points to /program/perl_v5.8.4/bin/perl -- latest version
So, when I have to run my perl script I have to manually specify in command prompt
/program/perl_v5.6.1/bin/perl scriptName.pl
I tried adding following lines in script:
/program/perl_v5.6.1/bin/perl
use v5.6.1;
But, this means script has to take Perl version > 5.6.1
I found couple of related question which suggested:
To export path. But, I want the script for all the users without have to export path
Specify version greater than. But, I want to use just one specific version for this script.
use/require commands. Same issue as above.
My question: How to specify in the script to only use a specific version of perl?
The problem is that the interpreter specified in the shebang line (#!/some/path/to/perl) is not used if perl script is called like this:
perl some_script.pl
... as the 'default' (to simplify) perl is chosen then. One should use the raw power of shebang instead, by executing the file itself:
./some_script.pl
Of course, that means this file should be made executable (with chmod a+x, for example).
I have in my code:
our $LEVEL = '5.10.1';
our $BRACKETLEVEL = sprintf "%d.%03d%03d", split/\./, $LEVEL;
if ($] != $currentperl::BRACKETLEVEL)
{
die sprintf "Must use perl %s, this is %vd!\n", $LEVEL, $^V;
}
These are actually two different modules, but that's the basic idea. I simply "use correctlevel" at the top of my script instead of use 5.10.1; and I get this die if a developer tries using the wrong level of perl for that product. It does not, however, do anything else that use 5.10.1; would do (enable strict, enable features like say, switch, etc.).

How do I run my first Perl code on a Windows 7 system?

I have run this Perl code:
#!/usr/bin/perl
print "content-type: text/html \n\n";
print "Hello World.\n";
I have tried it in two ways, first one is Testing your Perl installation, but when I run by this way, it has some troubles, it asks me to choose a program with which I can run it, but no running yet.
Second way is first script with Padre, the Perl IDE, but when I write Perl code and try to save it, it does not show me Perl file's extension, so I can't save it as Perl file, so what could I do?
Your code looks like you want a CGI program. CGI means that you call your program through a web browser and get a website back. While vstm's comment was of course right for non-cgi programs, your example requires a little more stuff in order to work like that.
You will need to install a web server. Take a look at xampp. It is simple to install and maintain and comes with a mysql as well as an apache installation. I recommend the lite version since that does not have all the overhead.
Once you've installed it, you need to make some configuration so it can run your perl scripts. I take it you have already installed Active Perl. You then need to tweak the apache config.
in c:\xampp\apache\conf\httpd.conf you need to find the line that says
<Directory "C:/xampp/htdocs">
and read the comments (marked with #). You have to add ExecCGI inside the <Directory> section. Do that for every directory you want perl scripts to be run. Then look for a line that says
AddHandler cgi-script .cgi .pl .asp
and make sure it is not commented out.
Once you're done, place your program in the c:\xampp\htdocs folder (cgi-bin should also work) and change the shebang-line (the first line with #!) to where you've installed Active Perl, e.g. C:\perl\bin\perl.exe. It tells apache what program it should use to execute the perl script.
Also, add some more lines to your code:
#!C:\perl\bin\perl.exe
use strict;
use warnings;
use CGI;
use CGI::Carp('fatalsToBrowser');
print "Content-type: text/html \n\n";
print "Hello World.\n";
Now you need to run the apache web server. In the xampp installation dir there are several batch files that control apache and mysql. There's also a xampp-control.exe. Run it. In the new window, click on the Start button next to Apache.
In your browser, go to http://localhost/<yourscript.pl>. It should now say "Hello World!".
If it does not, make sure you're not running Skype. It blocks your port 80 which the apache tries to run on. You need to change apache's port to something else. Refer to this video.
A few words on the changes in the code I made and what they do:
use strict; should always be in your code. It forces you to honor certain guidelines and to write better code. This might seem strange in a Hello World program, but please do it anyway.
use warnings; tells you about things that might go wrong. Warnings are not errors but rather perl being helpful about stuff you might not know yourself. Use it.
use CGI makes the program's output go to the web server. You need that when you work with CGI programs.
print "Content-type: text/html \n\n"; is needed so the browser knows what to expect. In this case, an HTML website. It is called the HTTP-Header and contains a mime-type.
use CGI::Carp('fatalsToBrowser'); makes errors go to the browser. Without it, you'd never know about them unless you look in apache's error log.

Why doesn't my Perl CGI script work?

I really do not get how to run a Perl file. I have uploaded my .pl to the cgi-bin then chmod to 755. Then when i go to run the file i just get a 500 internal server error.
**/cgi-bin/helloworld.pl**
#!/usr/bin/perl
print 'hello world';
Any ideas as to what I am doing wrong?
Read the official Perl CGI FAQ.
That'll answer this, and many other questions you may have.
For example: "My CGI script runs from the command line but not the browser. (500 Server Error)"
Hope this helps!
You probably need something like
print "Content-type: text/html\n\n";
before your print statement. Take a look at http://httpd.apache.org/docs/2.0/howto/cgi.html#troubleshoot
It would help to know what server you are using, and the exact error message that's showing up in the server's logs. I'd guess that, if you are using Apache, you'll see something like "Premature end of script headers".
Look into using CGI::Carp to output fatal errors to the browser. use CGI::Carp qw(fatalsToBrowser);
Also, please definitely do use the CGI module to output any needed information such as headers/html/whatever. Printing it all is the wrong way to do it.
EDIT: You will also definitely be able to check an error log of some sort.
Perhaps you need my Troubleshooting Perl CGI scripts
First, find out the path to perl on that system and make sure the shebang line is correct. Giving more information about the system and the web server would also help others diagnose.
Then, try:
#!/path/to/perl/binary
use strict;
use warnings;
$| = 1;
use CGI qw( :default );
print header('text/plain'), "Hello World\n";
Make sure that you can run the script from a shell prompt, without invoking it through Perl. In other words, you should be able to go to your cgi-bin directory and type:
./helloworld.pl
and get output. If that doesn't work, fix that. In looking at the output, the first line must be:
Content-Type: text/html
(Or text/plain or some other valid MIME type.)
If that's not the case, fix that.
Then you must have an empty line before the body of your page is printed. If there's no empty line, your script won't work as a CGI script. So your total output should look like this:
Content-Type: text/html
hello world
If you can run your script and that's the output, then there's something weird going on. If Apache is not logging the error to an error_log file somewhere, then maybe there's some problem with it.
Did you enable Apache to server .pl files as CGI scripts? Check your Apache config file, or (quick but not guaranteed test) try changing the file extension to .cgi. Also, make sure your shebang line (#!) is at the very top. Finally, check the line endings are Unix if your server is Linux. And yes, test it from the command-line, and use strict; for better feedback on potential errors.