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

Hi, I just started learning Perl, and being given a small task to move all hard coded URLs in tt files to a Catalyst module file. The code in the module file,
sub download :Local :Args(0) { my ( $self, $c ) = @_; my $user_given_job_id = $c->request->params->{job_id}; my $impute_job = $c->user->jobViews->search({id => $user_given_job +_id, status => 10})->first(); $c->model('DB::task')->create({name => 'send_access_key', paramete +rs => {job_id => $impute_job->id()}, active => 1}); $c->stash->{template} = 'download.tt'; $c->stash->{jobid} = $impute_job->id(); $c->stash->{jobname} = $impute_job->name(); }
The task is to move the hard coded URL in tt file to POST a form to http://example.org/cgi-bin/download.pl to the module file. I can't figure out how can I achieve this. Thank you.

Replies are listed 'Best First'.
Re: How to move hard coded URL in tt files to a Catalyst module file
by Erez (Priest) on Nov 01, 2016 at 10:32 UTC

    move the hard coded URL in tt file to POST a form

    If you want to move any element from the template to the module, it's quite a simple matter of assigning it to a stash variable:

    $c->stash('action' => 'http://example.org/cgi-bin/download.pl');
    Then you can call it from the template:
    <form action="[% action %]" method="POST">

    However, for a better answer to your case, I suggest showing the template code as well, and maybe some more information about the task in hand.

    Principle of Least Astonishment: Any language that doesn’t occasionally surprise the novice will pay for it by continually surprising the expert

      Erez, your solution actually worked. It was my bad, there was a typo in the code. Thank you so much.
      Hi Erez, Thank you for your help. However, it didn't work for me some how. Here is the code for the tt file. Your solution actually worked. It was my bad, there was a typo in the code. Thank you so much. Regards, Alwyn
Re: How to move hard coded URL in tt files to a Catalyst module file
by Your Mother (Archbishop) on Nov 01, 2016 at 12:40 UTC

    Pretty sure I follow but as mentioned already, you have not given enough code to know for sure. The module file is going to be a Controller. Named something like, MyApp::Controller::Widget.

    In your template you will set the action of the form by using the dynamically generated action->URI of the controller–

    <form action="[% c.uri_for_action("/widget/download") %]" method="POST +">

    There are several different ways to do what you want. Catalyst is not what I would recommend for learning Perl. Sounds like this is work related so good luck but the learning curve here is quite steep and assumes Perl knowledge.

      Hi (I'd better not call your screen name), Thank you for your help. But I don't think it gonna work. It is not calling a sub in the Controller, it needs to pass the values in the form to the "download.pl" file. Correct me if I was wrong. You have much more experience in Perl than myself. I just started learning it. Regards, Alwyn

        So far there is no evidence of a script called download.pl in your examples, just the URL. I am skeptical it exists because it would mean you are mixing CGI with Catalyst and using nearly the same names. In your Catalyst application, instead of download.pl there will be a URL path like /app_name/controller_name/download or host:port/controller_name/download if you are running on a high port or using the development server. In those cases, my answer is the right one.

        Catalyst has many parts, including simple deployment, that are extremely difficult for a beginner and it looks like maybe you are reading Catalyst documentation and CGI documentation (perhaps from a webhost?) and trying to mix them. It can be done, it works very badly but it's possible. Your code is wrong for that approach too and I highly discourage it. :( Better to go with vanilla CGI if on a budget host where CGI is the only deployment option.

Re: How to move hard coded URL in tt files to a Catalyst module file
by haukex (Archbishop) on Nov 01, 2016 at 10:06 UTC

    Hi alwynpan,

    I'm not a Catalyst expert, so I can't say anything off the top of my head, except that I think it might be good if you show a Short, Self Contained, Correct Example, as I find the code snippet you posted a little hard to understand out of context.

    Regards,
    -- Hauke D