Problems using the HTML::Template module - perl

I'm unable to execute the HTML::Template function in the CGI.
I'm following a simple tutorial that I found here: http://metacpan.org/pod/HTML::Template
I created a new file on my server in the home path as test.tmpl.
I created a new file named frt.cgi ... (is that the issue here? should it be a different file extention??)
#!/usr/local/bin/perl -w
use HTML::Template;
# open the html template
my $template = HTML::Template->new(filename => '/test.html');
# fill in some parameters
$template->param(HOME => $ENV{HOME});
$template->param(PATH => $ENV{PATH});
# send the obligatory Content-Type and print the template output
print "Content-Type: text/html\n\n", $template->output;
I've modified the 1st line to reflect my host provided program path for perl. I don't know what the -w does I just know I've tried this with and without it. Also I've tried changing the code a bit like this:
use warnings;
use strict;
use CGI qw(:standard);
use HTML::Template;
I've searched...
https://stackoverflow.com/search?q=HTML%3A%3ATEMPLATE+&submit=search
https://stackoverflow.com/search?q=HTML%3A%3ATEMPLATE
https://stackoverflow.com/search?q=HTML%3A%3ATEMPLATE+PERL&submit=search
Yet I still do not see the answer.
I even searched google for .TMPL Encoding because I thought there may be some special type needed. Please help.

If you look in your server logs, you'll probably see an error message along the lines of:
HTML::Template->new() : Cannot open included file /test.html : file not found.
You need to provide the path on the file system, not a URI relative to the generated document.

First, you likely specified the wrong path - change /test.html to test.html.
Also, it is possible that there is no $ENV{HOME} variable in your system so set up flag die_on_bad_params to 0:
my $template = HTML::Template->new(
filename => 'test.html',
die_on_bad_params => 0,
);
Also, don't forget to mark your Perl file as executable by chmod 755.
Option -w makes Perl to enable warnings, so there is no point to write use warnings; afterwards.
You can check what Perl command line options do by using module B::Deparse, like this ($^W variable disables/enables warnings):
perl -w -MO=Deparse -e "print;"
This would print:
BEGIN { $^W = 1; }
print $_;

Related

How to pass a Windows path as a value using the Perl Tie::Cfg module

Iam using the Tie::Cfg module for getting user details in my automation.
Problem/Issue:
I have a configuration file which accept user details and path. I am able to print values from the configuration file in Linux, but in the case of Windows, due to backslashes I cannot get the correct value.
for example: /root/devel/Conf.ini
user=test
password=config
path_linux=\home\basic\
path_wind=C:\Users\rakesh\Documents
I created a module /root/devel/test.pm
use strict;
use warnings;
use Tie::Cfg;
use parent 'Exporter';
tie our %conf,'Tie::Cfg', READ =>"Conf.ini", WRITE=> "Conf.ini", MODE=> 0777;
our #EXPORT = qw(%conf);
1;
In my third Perl script /root/devel/local.pl I am just printing the configuration values:
#!/usr/bin/perl
use Tie::Cfg;
print "date : $conf{path_linux} and path : $conf{path_wind} \n";
Output:
$conf{path_linux} = \home\basic\
$conf{path_wind} = C:SERSRAKESHDOCUMENTS
Could you please help me out with this?
Tie::Cfg applies a eval to the data in the config file, so it is treated as a double-quoted Perl string. For instance, the \r in \rakesh is converted to a carriage-return.
You can make use of Tie::Config instead, which leaves the data untouched. You would write
use Tie::Config;
use Fcntl '/O_*/';
tie our %conf,'Tie::Config', O_RDWR;
or you can stick with Tie::Cfg and escape all the backslashes in your config file, like this
user=test
password=config
path_linux=/home/basic
path_wind=C:\\Users\\rakesh\\Documents

Importing environment variables to Perl

I'm not sure if importing is the right word to use. I'm a beginner in both Perl and Bash. I have set a variable on Bash, so when I do:
echo $PRDIR
it prints a string (It's a directory name)
I want to import that string to Perl, and I don't know how to do that. I've tried:
$varex = system("$PRDIR");
print "$varex";
And also
$varex = system("echo $PRDIR");
print "$varex";
but that doesn't work (I understand the last one, It prints "0" because that's echo's return value). I've also tried redirecting stdout to a variable but I couldn't.
If you want Bash to export a variable into the environment so it's accessible to programs, you can use the export builtin:
export PRDIR
Inside Perl, you would then access it using the %ENV hash:
my $varex = $ENV{"PRDIR"};
print "\$varex is: $varex\n";
Another solution to use the variable directly in perl :
In the shell :
$ export PRDIR=foobar
In perl :
#!/usr/bin/perl
use Modern::Perl;
use Env qw/PRDIR/;
say $PRDIR;
I guess you need something like this:
use Cwd 'abs_path';
use File::Basename;
my $self = abs_path($0);
my $bindir = dirname( abs_path($0) );
unless ($ENV{APP_ENV}) {
warn "No APP_ENV, will try to get from bin/env.sh";
exec("source $bindir/env.sh && /usr/bin/perl $self") || die "$!";
}
I have env.sh in my bin folder with following content:
export APP_ENV=development
The idea behind this approach is that I don't need to bother if I set my ENV variables before running my Perl code or forget to do it. I need just to run my Perl program and it will take care about preparing environment for itself.

How to execute one perl script from website in perl?

I am trying to run perl script that doing some things and creating files from web browser page in perl. I am using Windows 7.
This is source:
use CGI;
use warnings;
use strict;
print "Content-type:text/html; charset=utf-8\r\n\r\n";
print "<a href='./#'>START</a>";
system("C:\Perl\bin\perl C:\xampp\htdocs\xampp\bc\create_yaml.pl");
When I load this page it'll open cmd, but file what I want to run won't create any files. How can i find out that the script run or not? And how to run this script?
I try to change permission to file that I want to run but still it doesn't work.
Thanks for answers.
I will try to do simple example. But it doesnt create any file... hmmm whats wrong?
use CGI;
use strict;
use warnings;
print "Content-Type: text/html; charset=utf-8\n\n";
system("C:\\Perl\\bin\\perl C:\\xampp\\htdocs\\xampp\\vyber\\bc\\test\\create.pl");
source of create.pl:
open(INFO,">aaaaaaa.txt");
print INFO "voda";
close INFO;
I think your issue is that Windows uses \ for path names, but when you put it in quotes, you need to escape it, because it's a special character. You escape with \:
system("C:\\Perl\\bin\\perl C:\\xampp\\htdocs\\xampp\\bc\\create_yaml.pl");
Also, if your environmental path variables are set up correctly, you can just do this:
system("perl C:\\xampp\\htdocs\\xampp\\bc\\create_yaml.pl");
Or as amon pointed out, you can use forward slashes instead:
system("C:/Perl/bin/perl C:/xampp/htdocs/xampp/bc/create_yaml.pl");

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.

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