fpscolin has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks, first time posting. I am trying to convert thousands of HTML files to a single PDF using wkhtmltopdf (called through a Perl script); this works via the Windows 7 command line like so:

wkhtmltopdf file1.htm file2.htm output.pdf

My issue is that the command line reaches its character limit when I try to add my string of file names as an argument. My workaround so far is to split the string into ~5000 character sections, which works when typed out literally like so:

my $result = `wkhtmltopdf.exe $lists[0] $lists[1] $lists[2] $project.pdf`;

However, since the number of HTML files varies from project to project, I cannot hard code the $list indexes, since @lists could have any number of indexes. I have tried working around this using a for-loop to created a string like so:

# Split up master list into smaller strings so that command line l +imit is not reached (limit to around 5000 chars) my @lists = $masterList =~ /(.{5000,}? )/g; # build the string of filename groups for pdf conversion my $htmlFiles = ""; for (my $y = 0; $y < $#lists; $y++) { $htmlFiles.= "\$lists[$y] "; }

I then make the system call with backticks like so:

    my $result = `wkhtmltopdf.exe $htmlFiles $project.pdf`;

This creates a small empty pdf, giving no errors. I know the issue (I think)... the backticks make Perl replace $htmlFiles with the string $htmlFiles, but once that replacement is made, the string contents do not get interpolated (ie. $lists[0] remains $lists[0] and not the contents of the first index of @lists).

Since I am relatively new to Perl (2 months in), I'm lost and searching for solutions has been difficult. If Perl cannot perform these multi-level interpolations, can someone suggest a solution?


Thank you, Colin

Replies are listed 'Best First'.
Re: Interpolating string that contains variables to be interpolated
by toolic (Bishop) on Apr 13, 2015 at 18:22 UTC
    I think you want to get rid of the backslash and let the variable be interpolated in the double quotes. Change:
    $htmlFiles.= "\$lists[$y] ";

    to:

    $htmlFiles.= "$lists[$y] ";

    You could also simplify and use the array directly because it'll interpolate and add spaces for you ($"):

    my $result = `wkhtmltopdf.exe @lists $project.pdf`;

      Perfect! I don't know why I thought I had to escape the $, but your solution fixed my issue. Thank you very much!