Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Store date into variable

Posted on 2003-11-24
8
Medium Priority
?
387 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
Independent Software Vendors: 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 100 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
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…
Six Sigma Control Plans

596 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