Need some help...

W

warpman

I need some help. I'm new to perl and I have borrow the following
code. This sub keeps on given me the following date:

November 5, 103 at 10:16:33:

I don't understand why it keeps on given me the year as 1xx instead of
2 digits. Any help would be greatly appreciated. Thanks in advanced.



=====
sub get_variables {

if ($FORM{'followup'}) {
$followup = "1";
@followup_num = split(/,/,$FORM{'followup'});
$num_followups = @followups = @followup_num;
$last_message = pop(@followups);
$origdate = "$FORM{'origdate'}";
$origname = "$FORM{'origname'}";
$origsubject = "$FORM{'origsubject'}";
}
else {
$followup = "0";
}

if ($FORM{'name'}) {
$name = "$FORM{'name'}";
$name =~ s/"//g;
$name =~ s/<//g;
$name =~ s/>//g;
$name =~ s/\&//g;
}
else {
&error(no_name);
}

if ($FORM{'email'} =~ /.*\@.*\..*/) {
$email = "$FORM{'email'}";
}

if ($FORM{'subject'}) {
$subject = "$FORM{'subject'}";
$subject =~ s/\&/\&amp\;/g;
$subject =~ s/"/\&quot\;/g;
}
else {
&error(no_subject);
}

if ($FORM{'url'} =~ /.*\:.*\..*/ && $FORM{'url_title'}) {
$message_url = "$FORM{'url'}";
$message_url_title = "$FORM{'url_title'}";
}

if ($FORM{'img'} =~ /.*tp:\/\/.*\..*/) {
$message_img = "$FORM{'img'}";
}

if ($FORM{'body'}) {
$body = "$FORM{'body'}";
$body =~ s/\cM//g;
$body =~ s/\n\n/<p>/g;
$body =~ s/\n/<br>/g;

$body =~ s/&lt;/</g;
$body =~ s/&gt;/>/g;
$body =~ s/&quot;/"/g;
}
else {
&error(no_body);
}

if ($quote_text == 1) {
$hidden_body = "$body";
$hidden_body =~ s/</&lt;/g;
$hidden_body =~ s/>/&gt;/g;
$hidden_body =~ s/"/&quot;/g;
}

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);

if ($sec < 10) {
$sec = "0$sec";
}
if ($min < 10) {
$min = "0$min";
}
if ($hour < 10) {
$hour = "0$hour";
}
if ($mon < 10) {
$mon = "0$mon";
}
if ($mday < 10) {
$mday = "0$mday";
}

$month = ($mon + 1);

@months = ("January","February","March","April","May","June","July","August","September","October","November","December");

if ($use_time == 1) {
$date = "$hour\:$min\:$sec $month/$mday/$year";
}
else {
$date = "$month/$mday/$year";
}
chop($date) if ($date =~ /\n$/);

$long_date = "$months[$mon] $mday, $year at $hour\:$min\:$sec";
# $long_date = "$months[$mon] $mday, 19$year at $hour\:$min\:$sec";
}
 
X

Xaonon

warpman said:
I need some help. I'm new to perl and I have borrow the following
code. This sub keeps on given me the following date:

November 5, 103 at 10:16:33:

I don't understand why it keeps on given me the year as 1xx instead of
2 digits. Any help would be greatly appreciated. Thanks in advanced.

perldoc -f localtime

-> $year is the number of years since 1900. That is, $year is 123 in year
-> 2023.... The proper way to get a complete 4-digit year is simply:
->
-> $year += 1900;
 
X

Xaonon

warpman said:
I need some help. I'm new to perl and I have borrow the following
code. This sub keeps on given me the following date:

November 5, 103 at 10:16:33:

I don't understand why it keeps on given me the year as 1xx instead of
2 digits. Any help would be greatly appreciated. Thanks in advanced.

perldoc -f localtime

-> $year is the number of years since 1900. That is, $year is 123 in year
-> 2023.... The proper way to get a complete 4-digit year is simply:
->
-> $year += 1900;
->
-> And to get the last two digits of the year (e.g., '01' in 2001) do:
->
-> $year = sprintf("%02d", $year % 100);
 
E

Eric Schwartz

I need some help. I'm new to perl and I have borrow the following
code. This sub keeps on given me the following date:

November 5, 103 at 10:16:33:

I don't understand why it keeps on given me the year as 1xx instead of
2 digits. Any help would be greatly appreciated. Thanks in advanced.

If in doubt, read the documentation:

$ perldoc -f localtime

If you get undesired output from a function, it's best to figure out
whether its your expectations or its that are being confounded.

In this specific case, I'd recommend you use the strftime() function
from the POSIX module. 'perldoc POSIX' for more info.

-=Erc
 
U

Uri Guttman

put the subject in the subject of the post.

did you steal/borrow/copy this code or create this mess yourself? it
looks like typical old cargo cult crap.

this code is so perl4ish and so clunky.

jeez, a y2k bug 3 years later.

rtfm some more on localtime.

no strict

some unseen and probably bad cgi parser is used

quoted scalar variables

it is not worth fixing. dump it all and rewrite it.

you should learn some perl from a better source too.

uri
 
G

Gunnar Hjalmarsson

warpman said:
... I have borrow the following code.

You should not use fragments of other people's code if you don't
understand how it works!
This sub keeps on given me the following date:

November 5, 103 at 10:16:33:

I don't understand why it keeps on given me the year as 1xx instead
of 2 digits.

Well, you have this line:

$long_date = "$months[$mon] $mday, $year at $hour\:$min\:$sec";

and the value of $year was grabbed via:

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
= localtime(time);

To understand how a Perl function such as localtime() works, you'd
better study its documentation:

http://www.perldoc.com/perl5.8.0/pod/func/localtime.html

That should give you what you need to solve the problem.
 
T

Tad McClellan

warpman said:
Subject: Need some help...


Please put the subject of your article in the Subject of your article.

This sub keeps on given me the following date:

November 5, 103 at 10:16:33:

I don't understand why it keeps on given me the year as 1xx instead of
2 digits.


Are you really using four year old code?

It is pretty late in the game for fixing y2k problems...

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);


You should read the documentation for the functions that you use:

perldoc -f localtime

Note that the $year element is not simply the last
two digits of the year. If you assume it is, then
you create non-Y2K-compliant programs--and you
wouldn't want to do that, would you?

The proper way to get a complete 4-digit year is
simply:

$year += 1900;

And to get the last two digits of the year (e.g.,
'01' in 2001) do:

...

if ($sec < 10) {
$sec = "0$sec";
}


This code is worth what you paid for it (or less).
 
G

Glenn Jackman

warpman said:
I need some help. I'm new to perl and I have borrow the following
code. This sub keeps on given me the following date:

November 5, 103 at 10:16:33:
[...]

Put this line at top of your script:
use POSIX qw(strftime);

Then, replace all the stuff below with these 3 lines:

my @now = localtime;
$date = strftime(($use_time ? '%T ' : '') . '%m/%d/%Y', @now);
$long_date = strftime('%B %e, %Y at %T', @now);


Read up on chomp() in place of your "chop() if ...";

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);

if ($sec < 10) {
$sec = "0$sec";
}
if ($min < 10) {
$min = "0$min";
}
if ($hour < 10) {
$hour = "0$hour";
}
if ($mon < 10) {
$mon = "0$mon";
}
if ($mday < 10) {
$mday = "0$mday";
}

$month = ($mon + 1);

@months = ("January","February","March","April","May","June","July","August","September","October","November","December");

if ($use_time == 1) {
$date = "$hour\:$min\:$sec $month/$mday/$year";
}
else {
$date = "$month/$mday/$year";
}
chop($date) if ($date =~ /\n$/);

$long_date = "$months[$mon] $mday, $year at $hour\:$min\:$sec";
# $long_date = "$months[$mon] $mday, 19$year at $hour\:$min\:$sec";
}
 
G

Gunnar Hjalmarsson

Desmond said:
Are you sure about that ? It'd mean that I'd use others' code ... erm ...
never.

Well, I said *fragments of* ... I meant to distinguish between using
a module, which is a complete whole intended to be used by others, and
just copying a fragment of code from somewhere.

Sorry if I was unclear.
 
B

Ben Morrow

Desmond Coughlan said:
Are you sure about that ? It'd mean that I'd use others' code ... erm ...
never.

Absolutely!

Note there is an important difference between copy/pasting other
people's code and writing to a documented API. The API is what
relieves you of the responsibility to understand what is going on
and places it on the maintainer.

Ben
 
D

David K. Wall

warpman said:
sub get_variables {

if ($FORM{'followup'}) {
$followup = "1";
@followup_num = split(/,/,$FORM{'followup'});
$num_followups = @followups = @followup_num;
$last_message = pop(@followups);
$origdate = "$FORM{'origdate'}";
$origname = "$FORM{'origname'}";
$origsubject = "$FORM{'origsubject'}";
}
[snip rest of code]

Looks familiar. Checking... yup, it's code from WWWBoard from Matt's
Script Archive. Some better-quality replacements are available at
http://nms-cgi.sourceforge.net/.

If you're trying to learn Perl, please don't use Matt's scripts as a
model. If you search the Google usenet archive* you'll find plenty of
past discussions on why this code is so reviled.

* http://groups.google.com/
 
D

David K. Wall

Anno Siegel said:
Few people would claim that without qualification.

Maybe he means it in a Biblical sense. I've screwed around with Perl,
and it has certainly screwed with my head at times.
 
W

warpman

I would like to thank all of you for your recommendations, suggestions
and even criticism. Keep in mind that I'm new to PERL. I'm learning as
I go. I took a look at the documentation and made some changes and
everything seems to be going OK at the moment. Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top