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

Hi Monks,
Hoping you can help me out as I've been unable to resolve an issue I'm having.

I have a CGI script that is calling a rather simple javascript file. Both are stored locally on the same server in the same directory. The script (the CGI part) runs fine and does what it's suppose to but when the javascript portion is called it throws the following error:

Not a CODE reference at /xxxx/www/docs/xxxx/internal/xxxx/test.cgi line 48.

Here's the portion of the code calling the js script.

if ('20'.$Menu_Year == $Default_Year && $Menu_Month >= $Default_Mo +nth && $Menu_Day >= $Default_Day) { print $cgi->(-title=>'Pop-up Window', -script=>{-language=>'javascript', -type=>'text/javascript', -src=>'http://s-internal.xxxx.com/xxxx/pop-up.js'});

It's just a simple alert window letting the user know that the date is out of bounds/not valid. I tried putting the code directly into the perl script but haven't had any luck. I'm using the alert\window.alert java function. I'd be open to other methods that accomplish the same task as well - looking into a few of those right now (so far no luck). Permissions on the server are not an issue (775).

Replies are listed 'Best First'.
Re: Perl, CGI, and calling a JS (javascript) file
by tobyink (Canon) on Mar 28, 2012 at 12:30 UTC

    Your error has nothing to do with Javascript. You're doing this:

    $cgi->(...)

    That is the syntax for executing a coderef. But $cgi is not a coderef (it's presumably a CGI object).

    It's not exactly clear what you want to happen here, but you may have meant:

    print $cgi->start_html( -title => 'Pop-up Window', -script => { -language => 'javascript', -type => 'text/javascript', -src =>'http://s-internal.xxxx.com/xxxx/pop-up.js', } );

    And by the way, Perl will not be "calling" the Javascript file. It will simply output an HTML <script> element which asks the browser to download and execute the script.

    Aside: It is certainly possible (via things like JE or JavaScript::SpiderMonkey) for Perl to call javascript code, but that's definitely not what you want here.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Perl, CGI, and calling a JS (javascript) file
by Anonymous Monk on Mar 28, 2012 at 12:26 UTC
    print $cgi->(

    What do you expect this to do? It seems there is a method name missing.