Solved

Store date into variable

Posted on 2003-11-24
8
378 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now