Solved

Store date into variable

Posted on 2003-11-24
8
382 Views
Last Modified: 2009-12-16
I am trying to get the date stored into a variable and then be able to display it over the web page.  I have this code.

#!/usr/bin/perl
sub date_format{
 
($sec,$min,$hour,$wday,$month,$year,$mday)=localtime(time());
 
$month = $month + 1;
 
$month="0".$month if ($month)<10;
#make month in 2 digits if it is less than 2
 
$wday="0".$wday if ($wday)<10;
#make date in 2 digits if it is less than 2
 
$yr=substr($year,1);
 
$date=$month."".$wday."".$yr;
 
return $date;
}
 
$fdate = &date_format;
 
print $fdate;

and when I run it from a command line it works, however when i enter the same code in and try to get a variable and display the variable over the browser I get an error here is the code.


#!/usr/bin/perl -w
use strict;
use CGI qw(:standard);
 
my $vendor = param('vendor');
my $vitem_num = param('vitem-num');
my $item = param('item');
my $i_desc = param('i-desc');
my $pak = param('pak');
my $cost = param('cost');
my $cost2 = param('cost2');
my $retail = param('retail');
my $rpt_code = param('rpt-code');
my $line_num = param('line-num');
my $upc = param('upc');
 
sub date_format{
 
($sec,$min,$hour,$wday,$month,$year,$mday)=localtime(time());
 
#$month = $month + 1;
 
$month="0".$month if ($month)<10;
#make month in 2 digits if it is less than 2

$wday="0".$wday if ($wday)<10;
#make date in 2 digits if it is less than 2
 
 
$yr=substr($year,1);
 
 
$date=$month."".$wday."".$yr;
 
return $date;
}
 
$fdate = &date_format;
 
print $fdate;
 
 
open(FILE, ">>/usr/tomax/data/UL.test") or die "Can't open file";
 
printf(FILE  "$vendor|$vitem_num|$item|$i_desc|$pak|$cost|$cost2|$retail|$rpt_code|$line_num|$upc\n");
 
close FILE;

print header();
 
 
print start_html('Sku Request');
 
print "<TABLE>
         <TR>
           <TD>&nbsp;1) Vendor Code: </TD>
           <TD>", param('vendor'), "</TD>
         </TR>
         <TR>
           <TD>&nbsp;2) Vendor Item #:&nbsp; <BR></TD>
           <TD>", param('vitem-num'), "</TD></TR>
         <TR>
           <td>&nbsp;3) Specific Item:<BR></TD>
           <td>", param('item'), "</TD></TR>
         <TR>
           <td>&nbsp;4) Item Desc:<BR></TD>
           <td>", param('i-desc'), "</TD></TR>
         <TR>
           <td>&nbsp;5) Pack:<BR></TD>
           <td>", param('pak'), "</TD></TR>
         <TR>
           <td>&nbsp;6) Cost:<BR></TD>
..........

         </TABLE>";
 
 
print end_html;

Does anyone know why this is?




0
Comment
Question by:jmsloan
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 20

Expert Comment

by:jmcg
ID: 9814550
Note that your command-line version does not say

use strict;

which allows your first script to work just fine even though the $month, etc variables are undeclared. In your second, CGI, script, you'd most likely get a "Premature end of script headers" error because Perl reports these undeclared variables in diagnostic messages.

It's probably sufficient to just say "my" on this line:


my ($sec,$min,$hour,$wday,$month,$year,$mday)=localtime(time());
 
0
 
LVL 18

Expert Comment

by:kandura
ID: 9815058
Not quite; these are the print statements as they appear:

print $fdate;
print header();
print start_html('Sku Request');

You must at least postpone the print $fdate;  until after you have started the html document.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 9816765
use POSIX;

replace the date_format subroutine with anyone of these POSIX date formats.

$date = strftime('%m%d%y', (localtime(time)));  # date formatted as: 112503
$date = strftime('%m%d%Y', (localtime(time)));  # date formatted as: 11252003
$date = strftime('%m%A%Y', (localtime(time)));  # date formatted as: 11Tuesday2003
$date = strftime('%m%a%y', (localtime(time)));  # date formatted as: 11Tue03
$date = strftime('%B %A %Y', (localtime(time))); # date formatted as: Tuesday November 2003


http://search.cpan.org/~nwclark/perl-5.8.2/ext/POSIX/POSIX.pod
http://search.cpan.org/~roode/Time-Format-0.13/Format.pm
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 28

Expert Comment

by:FishMonger
ID: 9816786
I should have mentioned, my suggestion doesn't solve your error (kandura's should do that), but it will clean up you code to make it more readable.
0
 
LVL 3

Author Comment

by:jmsloan
ID: 9817863
I took out all my date code and put in

$date = strftime('%m%d%y', (localtime(time)));

The browser will come up with an error saying

"Premature end of script headers: skureqsend.pl "

When I comment out "$date = strftime('%m%d%y', (localtime(time)));"

The program will work.

Any ideas now?
0
 
LVL 18

Expert Comment

by:kandura
ID: 9817909
did you move the 'print $date;' statement to somewhere _after_ you 'print header();' ?
0
 
LVL 3

Author Comment

by:jmsloan
ID: 9817929
Right now I don't even have the print date command anywhere.  Here is what the code is now.

#!/usr/bin/perl -w
use strict;
use CGI qw(:standard);
 
my $vendor = param('vendor');
my $vitem_num = param('vitem-num');
my $item = param('item');
my $i_desc = param('i-desc');
my $pak = param('pak');
my $cost = param('cost');
my $cost2 = param('cost2');
my $retail = param('retail');
my $rpt_code = param('rpt-code');
my $line_num = param('line-num');
my $upc = param('upc');
 
$date = strftime('%m%d%y', (localtime(time)));
 
open(FILE, ">>/usr/tomax/data/UL.test") or die "Can't open file";
 
printf(FILE  "$vendor|$vitem_num|$item|$i_desc|$pak|$cost|$cost2|$retail|$rpt_code|$line_num|$upc\n");
 
close FILE;
 
print header();
 
print start_html('Sku Request');
 
print "<TABLE>
         <TR>
           <TD>&nbsp;1) Vendor Code: </TD>
           <TD>", param('vendor'), "</TD>
         </TR>
............
           <td>11) UPC:<BR></TD>
           <td>", param('upc'), "</TD>
         </TR>
         </TABLE>";
 
 
print end_html;

0
 
LVL 18

Accepted Solution

by:
kandura earned 25 total points
ID: 9818010
ah. you haven't imported the POSIX library that contains the strftime function. Add
   use POSIX;
at the top of the program.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

829 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