Solved

Store date into variable

Posted on 2003-11-24
8
379 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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