0

I am working with Perl/CGI, MySQL, Perl Template toolkit.

I have a database set up and some perl modules to query the database.

From the perl modules I pass on a datastructure (hash of hashes) to perl template toolkit (.tt) and that renders the results on the webpage.

I would now like to add an option of downloading the search results into a tab delimited file; i.e. provide a download file option. I have a subroutine in my perl module to do the conversion into tab-seperated format. I want to be able to call that subroutine to convert the search results into tab-seperated format. Can I call a subroutine from a perl module in Template toolkit?

I am trying to figure out how to generate a downloadable file without again querying the database or without storing the results in CACHE.

Is there a way to pass the datastructure(hash of hashes) that Perl template is rendering, to a Javascript (that further calls subroutine) that can then generate a downloadble file?

Please suggest a correct approach.

Thanks for your time

Sashi Kiran Challa
  • 905
  • 1
  • 11
  • 21

1 Answers1

3

Can I call a subroutine from a perl module in Template toolkit?

You can, but it doesn't make sense to for this problem.

You don't need any templating capabilities, and you do need a different Content-Type header. Don't use TT when the tab separated file is being created.

I am trying to figure out how to generate a downloadable file without again querying the database or without storing the results in CACHE.

There is no reasonable way to do that. The closest you could get would be to parse the data out of the generated (by TT) HTML document with JavaScript (not using the Perl you wrote to generate the tab separated file), and then build the tab separated file on the client, and make it available for download.

It would be simpler, easier and more reliable to just hit the database again.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Another way would be to save the CSV file (let's call it that) every time the page is created and offer a download link. If the user actually uses it is up to him. Files would need to be cleaned up once in a while, and individual user's files would have to be individually named so they don't overwrite each other. – simbabque Jun 07 '12 at 10:58