I am trying to autogenerate reports based from statistics acquired from our database. I use perl to communicate with the database and store the interesting variables as scalars/arrays etc. I would then like to create a latex file before using pdflatex. However, I am having some issues trying to pass
scalar values into the pdf. For example, please see my code below and the output
#!/usr/bin/perl
#
#
# Description: Write a monthly report from the database using latex
#
# Parameters:
#
# History:
#
# CVS ID: $Date: 2012/03/01 16:13:03 $ $Revision: 1.1 $
########################################################################
#Add library
use lib "$ENV{HOME}/perllib";
#Call modules
use DBI; # Postgres communication functions
use File::Temp qw/tempfile/;
use File::Copy;
use Cwd;
my $date_start = '01/12/12';
my $date_stop = '31/12/12';
%hash=("01"=>"Jan","02"=>"Feb","03"=>"Mar","04"=>"Apr","05"=>"May","06"=>"Jun","07"=>"Jul","08"=>"Aug","09"=>"Sep","10"=>"Oct","11"=>"Nov","12"=>"Dec");
$date_start=~/([0-9]{2})\/([0-9]{2})\/([0-9]{2}).*/;
my $date_string = "$hash{$2}$3";
my $report_name = "$date_string"."_monthly_report.pdf";
#Pragmas
use warnings;
no warnings "uninitialized"; # Don't warn about unitialized variables#
use strict 'vars'; # Force all variables to have defined scope
my $repdir = "/home/nm/Desktop/";
my ($fh, $filename) = tempfile( SUFFIX => '.tex', DIR => $repdir);
# LaTeX Header information
print $fh <<'END';
\documentclass{article}
\usepackage{underscore}
\usepackage{fullpage}
\usepackage{multicol}
\usepackage[margin=0.5in]{geometry}
\usepackage{graphicx}
\usepackage{array}
\newenvironment{nstabbing}
{\setlength{\topsep}{0pt}%
\setlength{\partopsep}{0pt}%
\tabbing}
{\endtabbing}
\begin{document}
\begin{center}
\LARGE \bf MONTHLY SUMMARY
\end{center}
END
my $blank = " ";
# Body of LaTeX document
print $fh <<END;
This report details clinical and imaging statistics from $date_start to $date_stop
END
print $fh <<'END';
This report details clinical and imaging statistics from $date_start to $date_stop
\large \bf Clinical Statistics
\normalsize \rm
\begin{table}[h]
\centering
\begin{tabular}{cccc}
\hline
Task & AR & DB & GM \\
Cannulation & 1 & 2 & 3 \\
\hline
\end{tabular}
\end{table}
END
# Closing of LaTeX document
print $fh <<'END';
\end{document}
END
# Run LaTeX compiler to generate PDF
system('pdflatex -output-directory ' . $repdir . ' ' . $filename);
my $file_prefix = substr($filename, 0, -4);
#remove aux, log and tex files
my $auxfile = $file_prefix . ".aux";
my $logfile = $file_prefix . ".log";
my $pdffile = $file_prefix . ".pdf";
system('rm ' . $auxfile . ' ' . $logfile . ' ' . $filename);
$filename="$repdir$report_name";
move($pdffile,$filename);
system('acroread ' . $filename);
Why does
print $fh <<END;
This report details clinical and imaging statistics from $date_start to $date_stop
END
give the correct output, while the following does not?
print $fh <<'END';
This report details clinical and imaging statistics from $date_start to $date_stop
END
I tried using the former to create the table on display but I could not get it in a nice format, e.g. it would create an extra column and place cannulation beside GM. Also when using the former I had to place an extra / before latex keywords, eg //bf rather than /bf whereas in the latter (‘END’) I don’t need to.
Ideally I’d prefer to use ‘END’ as my tables and latex formatting work as expected. However, I am unable to pass scalar and array values. How can I manage this?
The single quotes around
'END'mean that your here document is treated like a single-quoted string: no variables are interpolated within it.The default is to treat as a double quoted string, which is why your first example works. You can also use double quotes to explicitly specify that you want it to be like a double quoted string:
<<"END";(This is like the default behavior, but perhaps clearer).An inevitable consequence of using an interpolating (double quoted) string is that you have to escape things like backslashes–backslashes have a special meaning within this kind of string. You really have to pick one or the other: a string that interpolates variables and requires escaping, or a string that does not interpolate and does not require escaping.