Solved

Store date into variable

Posted on 2003-11-24
8
383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Strange perl issue 6 132
perl split by | 2 109
How to generate square thumbnail using perl 13 90
Call Shell Script from Perl Script 6 163
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

762 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