?
Solved

Exec Format Error when calling an HTML file from a perl cgi script

Posted on 2004-11-15
7
Medium Priority
?
14,972 Views
Last Modified: 2013-12-20
I have a cgi script that uses some javascript functions to display a popup calendar for choosing dates. Thejavascript calls an HTML file named calendar.html as the base for the calendar. Whenever I click on the icon to open the calendar, the new window opens, but with an Error 500 Internal Server Error. Checking the apache error log I see:

Exec format error: exec of '/v/www/cgi-bin/timesheet/calendar.html' failed.

I know this has something to do with perl not knowing how to open or handle the file, or something like that.

So is it possible to execute/open/display an HTML file from within a perl script, or am I missing something here?

Thanks

Paul
0
Comment
Question by:PaulS_III
  • 4
  • 3
7 Comments
 
LVL 13

Accepted Solution

by:
gripe earned 500 total points
ID: 12584065
It sounds like you're trying to 'exec()' your HTML file instead of printing it's contents. exec() is for 'executing' other executable programs, such as another perl script. Also, exec() will execute it's argument and -never return-, so the rest of your script will not be completed after a successful exec. If you want to output the contents of the file, you should open() the file and print it's contents, like this:

open my $html, '/v/www/cgi-bin/timesheet/calendar.html' or die $!;
while ( <$html> ) { print; }

You should also consider using a templating module to do this stuff. They are built for your very requirements and have the added bonus of allowing you to include and substitute variables or code blocks within your templates.

Some suggested template modules are:

HTML::Template
Template::Toolkit
Text::Template

Hope that helps.
0
 
LVL 2

Author Comment

by:PaulS_III
ID: 12584143
gripe,
Thanks, that does make sense. Now let me go a few steps farther. Here is the entire perl function I am using to generate this popup calendar:

my $SCRIPT=<<END;
<script language="JavaScript">
var NUM_CENTYEAR = 30;
var BUL_TIMECOMPONENT = false;
var BUL_YEARSCROLL = true;
                                                                                                                         
var calendars = [];
var RE_NUM = /^\-?\d+\$/;
                                                                                                                         
function calendar2(obj_target) {
                                                                                                                         
        this.gen_date = cal_gen_date2;
        this.gen_time = cal_gen_time2;
        this.gen_tsmp = cal_gen_tsmp2;
        this.prs_date = cal_prs_date2;
        this.prs_time = cal_prs_time2;
        this.prs_tsmp = cal_prs_tsmp2;
        this.popup    = cal_popup2;
                                                                                                                         
        if (!obj_target)
                return cal_error("Error calling the calendar: no target control specified");
        if (obj_target.value == null)
                return cal_error("Error calling the calendar: parameter specified is not valid target control");
        this.target = obj_target;
        this.time_comp = BUL_TIMECOMPONENT;
        this.year_scroll = BUL_YEARSCROLL;
                                                                                                                         
        this.id = calendars.length;
        calendars[this.id] = this;
}
                                                                                                                         
function cal_popup2 (str_datetime) {
        this.dt_current = this.prs_tsmp(str_datetime ? str_datetime : this.target.value);
        if (!this.dt_current) return;
                                                                                                                         
        var obj_calwindow = window.open(
                'calendar.html?datetime=' + this.dt_current.valueOf()+ '&id=' + this.id,
                'Calendar', 'width=200,height='+(this.time_comp ? 215 : 190)+
                ',status=no,resizable=no,top=200,left=200,dependent=yes,alwaysRaised=yes'
        );
        obj_calwindow.opener = window;
        obj_calwindow.focus();
}
                                                                                                                         
function cal_gen_tsmp2 (dt_datetime) {
        return(this.gen_date(dt_datetime) + ' ' + this.gen_time(dt_datetime));
}
                                                                                                                         
function cal_gen_date2 (dt_datetime) {
        return (
                (dt_datetime.getMonth() < 9 ? '0' : '') + (dt_datetime.getMonth() + 1) + "/"
                + (dt_datetime.getDate() < 10 ? '0' : '') + dt_datetime.getDate() + "/"
                + dt_datetime.getFullYear()
        );
}
function cal_gen_time2 (dt_datetime) {
        return (
                (dt_datetime.getHours() < 10 ? '0' : '') + dt_datetime.getHours() + ":"
                + (dt_datetime.getMinutes() < 10 ? '0' : '') + (dt_datetime.getMinutes()) + ":"
                + (dt_datetime.getSeconds() < 10 ? '0' : '') + (dt_datetime.getSeconds())
        );
}
                                                                                                                         
function cal_prs_tsmp2 (str_datetime) {
        if (!str_datetime)
                return (new Date());
                                                                                                                         
        if (RE_NUM.exec(str_datetime))
                return new Date(str_datetime);
                                                                                                                         
        var arr_datetime = str_datetime.split(' ');
        return this.prs_time(arr_datetime[1], this.prs_date(arr_datetime[0]));
}
function cal_prs_date2 (str_date) {
                                                                                                                         
        var arr_date = str_date.split('/');
                                                                                                                         
        if (arr_date.length != 3) return alert ("Invalid date format: '" + str_date + "' Format accepted is dd-mm-yyyy");        if (!arr_date[1]) return alert ("Invalid date format: '" + str_date + "' No day of month value can be found");
        if (!RE_NUM.exec(arr_date[1])) return alert ("Invalid day of month value: '" + arr_date[1] + "' Allowed values are unsigned integers");
        if (!arr_date[0]) return alert ("Invalid date format: '" + str_date + "' No month value can be found");
        if (!RE_NUM.exec(arr_date[0])) return alert ("Invalid month value: '" + arr_date[0] + "' Allowed values are unsigned integers");
        if (!arr_date[2]) return alert ("Invalid date format: '" + str_date + "' No year value can be found");
        if (!RE_NUM.exec(arr_date[2])) return alert ("Invalid year value: '" + arr_date[2] + "' Allowed values are unsigned integers");
                                                                                                                         
        var dt_date = new Date();
        dt_date.setDate(1);
                                                                                                                         
        if (arr_date[0] < 1 || arr_date[0] > 12) return alert ("Invalid month value: '" + arr_date[0] + "' Allowed range is 01-12");
        dt_date.setMonth(arr_date[0]-1);
                                                                                                                         
        if (arr_date[2] < 100) arr_date[2] = Number(arr_date[2]) + (arr_date[2] < NUM_CENTYEAR ? 2000 : 1900);
        dt_date.setFullYear(arr_date[2]);
                                                                                                                         
        var dt_numdays = new Date(arr_date[2], arr_date[0], 0);
        dt_date.setDate(arr_date[1]);
        if (dt_date.getMonth() != (arr_date[0]-1)) return alert ("Invalid day of month value: '" + arr_date[1] + "' Allowed range is 01-"+dt_numdays.getDate()+"");
                                                                                                                         
        return (dt_date)
}
                                                                                                                         
function cal_prs_time2 (str_time, dt_date) {
                                                                                                                         
        if (!dt_date) return null;
        var arr_time = String(str_time ? str_time : '').split(':');
                                                                                                                         
        if (!arr_time[0]) dt_date.setHours(0);
        else if (RE_NUM.exec(arr_time[0]))
                if (arr_time[0] < 24) dt_date.setHours(arr_time[0]);
                else return cal_error ("Invalid hours value: '" + arr_time[0] + "' Allowed range is 00-23");
        else return cal_error ("Invalid hours value: '" + arr_time[0] + "' Allowed values are unsigned integers");
                                                                                                                         
        if (!arr_time[1]) dt_date.setMinutes(0);
        else if (RE_NUM.exec(arr_time[1]))
                if (arr_time[1] < 60) dt_date.setMinutes(arr_time[1]);
                else return cal_error ("Invalid minutes value: '" + arr_time[1] + "' Allowed range is 00-59");
        else return cal_error ("Invalid minutes value: '" + arr_time[1] + "' Allowed values are unsigned integers");
                                                                                                                         
        if (!arr_time[2]) dt_date.setSeconds(0);
        else if (RE_NUM.exec(arr_time[2]))
                if (arr_time[2] < 60) dt_date.setSeconds(arr_time[2]);
                else return cal_error ("Invalid seconds value: '" + arr_time[2] + "' Allowed range is 00-59");
        else return cal_error ("Invalid seconds value: '" + arr_time[2] + "' Allowed values are unsigned integers");
                                                                                                                         
        dt_date.setMilliseconds(0);
        return dt_date;
}
                                                                                                                         
function cal_error (str_message) {
        alert (str_message);
        return null;
}
</script>
                                                                                                                         
<form name=tstest>
<input type="Text" name="input5" value="">
<a href="javascript:cal5.popup();"><img src="/image/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the date"></a><br>
</form>
                                                                                                                         
<script language="JavaScript">
                                                                                                                         
var cal5 = new calendar2(document.forms['tstest'].elements['input5']);
cal5.year_scroll = true;
                                                                                                                         
cal5.time_comp = false;
                                                                                                                         
</script>
END
                                                                                                                         
print $SCRIPT;
}

This is the entire function I am using to generate my page and includes the javascript to generate the popup. So are you saying I can use one of the templating modules to do all of this somehow?

If so could you provide some direction on how I can do this? For one reason or another I am a bit gun shy towards the template modules.

Thanks
0
 
LVL 13

Expert Comment

by:gripe
ID: 12584621
I'm not sure what you're using perl for in this case, you seem to be just storing some static javascript into a variable and printing. Is this meant to be output within a larger CGI program or HTML file output? Am I missing something?

The thing with a template module is that it will allow you to separate your logic from your presentation. It often requires a different outlook on the overall design of your solution and layout of your site, but generally results in an easier to manage total package. The templating module will not automatically make everything better for you, but will enable you to store reusable pieces of presentation logic (IE: HTML/JavaScript/XML/etc) in standalone files that you can then include within your CGI (or mod_perl, etc) code to be used them as 'building blocks' to your overall design.

By doing this, you can seperate out your server side logic from your client side logic/presentation and reuse stuff (such as your javascript calendar app above) by embedding it wherever it's needed. This will also ensure that there is only one 'master' file to change to make presentation changes that will then be populated across your templated site as opposed to making many smaller configuration changes for every script with embedded presentation logic or HTML file in general.

I hope this is understandable. It may be the case that you don't require templating, but speaking from personal experience I just find it considerably easier to manage everything through templates. Let me know if you have any questions. In a lot of cases, some of the larger templating modules can also be overkill, such as Template::Toolkit. I would start small with 'Text::Template' and work up to it's limitations before going for the kitchen sink.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 2

Author Comment

by:PaulS_III
ID: 12584729
Yes this is part of a larger CGI application. I am writing a time tracking application where users can log in, add line items to tasks they are working on, and then print off an excel spreadsheet of they tasks they have been working on within some time frame. Enter this particular situation. I am adding ni this popup calendar to aide the users in picking a dtae range they wish to print off.

I think the light bulb lit up in my brain. I am now taking the HTML code for the text box and such and throwing that off into a template file to open and use using HTML::Template. Therefore my template file will look something like this:

<script language="JavaScript" src="calendar2.js>
</script>
                                                                                                                         
<form name=tstest>
<input type="Text" name="input5" value="">
<a href="javascript:cal5.popup();"><img src="/image/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the date"></a><br>
</form>
                                                                                                                         
<script language="JavaScript">
                                                                                                                         
var cal5 = new calendar2(document.forms['tstest'].elements['input5']);
cal5.year_scroll = true;
                                                                                                                         
cal5.time_comp = false;
                                                                                                                         
</script>

Then I can use HTML::Template to open and display this file. I am thinking this should work. My only thought/concern would be returning the date picked, but I will experiment with the template variables to try and get that value back.

Please allow me to experiment with this a bit before I award the points and close, but I think you have put me on the right track.

I will be very glad to look at any additional tips/information you are willing to pass on regarding this new approach.

Thanks

Paul
0
 
LVL 13

Expert Comment

by:gripe
ID: 12585128
Your form variables will be passed as before. So if you're using CGI.pm, you can do all of your form processing before choosing a template to output. HTML::Template will have no impact on how you normally process your form data.
0
 
LVL 2

Author Comment

by:PaulS_III
ID: 12585608
After some juggling some files around and getting things in the proper place, I got this to work. Thanks for pointing me to the template modules. This is super slick
0
 
LVL 13

Expert Comment

by:gripe
ID: 12586165
Glad you like it.. I'm sure it will make your job easier.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question