CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Perl code (SSI) to detect day and time

 



sangiro
Novice

Feb 13, 2002, 4:50 AM

Post #1 of 18 (5271 views)
Perl code (SSI) to detect day and time Can't Post

Hey all,

I'm looking for a piece of PERL that will return 'true' only on a certain day of the week for a certain time period. For instance between 9pm and 10pm every Wednesday night I want to display 'true' on a page via SSI.

1) Check the day and time
2) If day = Wednesday and time > 9pm and time < 10pm print "true"
3) Else print "false"

That's the logic. I just know too little about Perl to write it myself! Can you help?

Safe swoops
Sangiro

http://www.dropzone.com/


fashimpaur
User

Feb 13, 2002, 7:03 AM

Post #2 of 18 (5266 views)
Re: [sangiro] Perl code (SSI) to detect day and time [In reply to] Can't Post

Here's the core:

You may need to fix the first line if different AND running on Unix type Platform.


Code
 #!/usr/local/bin/perl 


use strict;
@ltime = localtime();


(($ltime[6] == 3) and ($ltime[2] == 21))?print "true":print "false";
exit;

HTH,

Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


Paul
Enthusiast

Feb 13, 2002, 8:12 AM

Post #3 of 18 (5265 views)
Re: [fashimpaur] Perl code (SSI) to detect day and time [In reply to] Can't Post

You don't need to "exit" at the end.

The less exits the better.


sangiro
Novice

Feb 14, 2002, 1:40 PM

Post #4 of 18 (5255 views)
Re: [fashimpaur] Perl code (SSI) to detect day and time [In reply to] Can't Post

Thanks a lot! Code seems to be working (not returning an error) expect that it's not printing anything.... thoughts?

Safe swoops
Sangiro

http://www.dropzone.com/


fashimpaur
User

Feb 14, 2002, 2:26 PM

Post #5 of 18 (5253 views)
Re: [sangiro] Perl code (SSI) to detect day and time [In reply to] Can't Post

SSI configuration error on server? That's my guess.

Good Luck,
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


sangiro
Novice

Feb 14, 2002, 2:46 PM

Post #6 of 18 (5252 views)
Re: [fashimpaur] Perl code (SSI) to detect day and time [In reply to] Can't Post

A very large part of my site is SSI based... basically use templates and pull all content in via SSI. Not sure why the server would not parse this one. Unix permissions are set to 755 on cgi file....

Safe swoops
Sangiro

http://www.dropzone.com/


Paul
Enthusiast

Feb 14, 2002, 3:10 PM

Post #7 of 18 (5251 views)
Re: [sangiro] Perl code (SSI) to detect day and time [In reply to] Can't Post

I wouldn't be surprised if the script is dying.

fashimpaur used strict but then forgot to declare the array Tongue

Try:

[perl]
#!/usr/bin/perl

use strict;

my @array = localtime();

print (($array[6] == 3) and ($array[2] == 21)) ? 'TRUE' : 'FALSE';
[/perl]


(This post was edited by RedRum on Feb 14, 2002, 3:10 PM)


fashimpaur
User

Feb 15, 2002, 4:44 AM

Post #8 of 18 (5246 views)
Re: [RedRum] Perl code (SSI) to detect day and time [In reply to] Can't Post

AARRRGGGHHHH!!!! Shocked Thanks RedRum. I ran the
code in the IDE, but did not run it with the -w flag turned on!

Sangiro. Sorry for the mistake. You can use RedRum's code
or insert (without the quotes) "my " in front of @ltime = localtime;
in the code I gave you and that will fix the error RedRum is talking
about.
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
print unpack"u*",pack "h*",$a,"\n\n";


sangiro
Novice

Feb 21, 2002, 2:29 PM

Post #9 of 18 (5237 views)
Re: [fashimpaur] Perl code (SSI) to detect day and time [In reply to] Can't Post

Thanks guys! Got it going. Had to add:

print "Content-type: text/html\n\n";

Can I add one more complication to my question: What would the code look like if I wanted to display "true" between between 9pm on Wednesday and 6am on Thursday (instead on the original 9-10 on Wednesday)?

Safe swoops
Sangiro

http://www.dropzone.com/

(This post was edited by sangiro on Feb 21, 2002, 3:01 PM)


yapp
User

Feb 23, 2002, 2:27 AM

Post #10 of 18 (5231 views)
Re: [sangiro] Perl code (SSI) to detect day and time [In reply to] Can't Post


In Reply To
What would the code look like if I wanted to display "true" between between 9pm on Wednesday and 6am on Thursday


Give it a try:
I guess this should work.

[perl]
use strict; # allways use this, get used to it!
use constant WEDNESDAY => 3;
use constant THURSDAY => 4;

# or gmtime() for GMT time
my(undef,undef,$hour,undef,undef,undef,$wday) = localtime();

if((WEDNESDAY <= $wday && $hour <= 9) && ($wday <= THURSDAY && $hour <= 18)) {
print "This is between wed 6:00 and thu 18:00\n";
}
else {
print "Definitely not!\n";
}
[/perl]

btw, "use constant X => 1" is just a cool way of saying "sub X(){1}", which results in a in-lined subroutine.

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool

(This post was edited by yapp on Feb 23, 2002, 2:29 AM)


Paul
Enthusiast

Feb 23, 2002, 2:39 AM

Post #11 of 18 (5224 views)
Re: [yapp] Perl code (SSI) to detect day and time [In reply to] Can't Post

Hmm using constants for that isn't really needed though :)


freddo
User

Feb 23, 2002, 8:58 PM

Post #12 of 18 (5217 views)
Re: [yapp] Perl code (SSI) to detect day and time [In reply to] Can't Post

Hi Yapp,


In Reply To
btw, "use constant X => 1" is just a cool way of saying "sub X(){1}", which results in a in-lined subroutine


This is just a note, the fact is that i was thinking the same as you about sub x {1}, but an article i saw earlier this month (i dont remember where, but i'll tell you) made me check the following testConstant.pl: [perl]use constant SECONDS_IN_DAY => 60 * 60 * 24;
use constant DEBUG_CONST => 0;

sub HOURS_IN_DAY { 24 }
sub DEBUG_SUB { 0 }

print "There have been ", int(time() / SECONDS_IN_DAY), " days since the epoch.\n";
print "There have been ", int(time() / HOURS_IN_DAY), " hours since the epoch.\n";
print "Debug Message\n" if DEBUG_CONST;
print "Debug Message\n" if DEBUG_SUB;
[/perl]

perl -MO=Deparse testConstant.pl will tell:

[perl]sub DEBUG_CONST () {
package constant;
$scalar;
}
sub SECONDS_IN_DAY () {
package constant;
$scalar;
}
sub HOURS_IN_DAY {
24;
}
sub DEBUG_SUB {
0;
}
print 'There have been ', int time / 86400, " days since the epoch.\n";
print 'There have been ', int time / HOURS_IN_DAY(), " hours since the epoch.\n";
'???';
print "Debug Message\n" if DEBUG_SUB ;[/perl]

There you can see that the constant have been replaced in the print statement while the sub didnt... Note the '???' it indicates that perl optimized away a constant value (and in this case a whole statement...). So i guess it's better to use constants than subs...

Anybody have a better knowledge on how this works?

Freddo
;---


mhx
Enthusiast

Feb 24, 2002, 1:56 AM

Post #13 of 18 (5215 views)
Re: [freddo] Perl code (SSI) to detect day and time [In reply to] Can't Post

Yep, the subroutine needs an empty prototype to be inlined:

[perl]
sub BADANSWER {
41;
}
sub ANSWER () {
42;
}
printf "the answer is %d\n", BADANSWER;
printf "the answer is %d\n", ANSWER;
[/perl]

will be turned into

[perl]
sub BADANSWER {
41;
}
sub ANSWER () {
42;
}
printf "the answer is %d\n", BADANSWER();
printf "the answer is %d\n", 42;
[/perl]

Also, if you prefix the call to the constant subroutine with an &, it is also not inlined by the optimizer. This is usually done wrong when using constant subroutines as hash keys:

[perl]
use constant ONE => 1;
use constant TWO => 2;

my %wrong = ( ONE => 'ONE', TWO => 'TWO' );
my %bad = ( &ONE => 'ONE', &TWO => 'TWO' );
my %good = ( ONE() => 'ONE', TWO() => 'TWO' );
my %also_ok = ( ONE , 'ONE', TWO , 'TWO' );

print "wrong: $good{ONE}\n";
print "bad: $good{&ONE}\n";
print "good: $good{ONE()}\n";
print "better: $good{+ONE}\n";
[/perl]

This will lead to the following optimized code:

[perl]
sub TWO () {
package constant;
$scalar;
}
sub ONE () {
package constant;
$scalar;
}
my(%wrong) = ('ONE', 'ONE', 'TWO', 'TWO');
my(%bad) = (&ONE, 'ONE', &TWO, 'TWO');
my(%good) = (1, 'ONE', 2, 'TWO');
my(%also_ok) = (1, 'ONE', 2, 'TWO');
print "wrong: $good{'ONE'}\n";
print "bad: $good{&ONE}\n";
print "good: $good{1}\n";
print "better: $good{1}\n";
[/perl]

As you can see, you cannot usually pass the hash key as a bareword, since it would be treated as a string then. When you use & to disambiguate, the optimizer won't inline your constant. It'll also pass the @_ array to the sub although it has a prototype. The 'right' way to disambiguate is to use an empty argument list or, if the context allows it, to prepend a unary plus.

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



yapp
User

Feb 24, 2002, 6:01 AM

Post #14 of 18 (5212 views)
Re: [RedRum] Perl code (SSI) to detect day and time [In reply to] Can't Post


In Reply To
Hmm using constants for that isn't really needed though :)

Yep, you could use scalars aswell here or even real values.
However, I normally find the last version most confusing.

Saying if(WEDNESDAY < $X) is better then if(3 < $X) isn't it?
At least you know where the 3 stands for (although replacing $X by $day might give you a hint Wink)


About that constant, I never knew that. However, it might improve readability for beginners. Saying sub WEDNESDAY(){3} isn't that clear. use constant is. It's your choice off course.Smile


However, my constants.pl file i've been using in my forum scripts also uses the sub(){} notation you preffer. Creating 400 inline subroutines is quite faster then processing 400 imports that generate 400 inline subroutines. Shocked

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool

(This post was edited by yapp on Feb 24, 2002, 6:03 AM)


yapp
User

Feb 24, 2002, 6:07 AM

Post #15 of 18 (5209 views)
Re: [mhx] Perl code (SSI) to detect day and time [In reply to] Can't Post


In Reply To
Also, if you prefix the call to the constant subroutine with an &, it is also not inlined by the optimizer.


I did not know it also worked with a &. I usually put a () at the end of the name. However, I need that sometimes: [perl]#!/usr/bin/perl -w

use strict;

#....

BEGIN {
require "intialize.pl";|
}

#...
[/perl]

initialize.pl
[perl]
require "constants.pl";
require "english.lang";

if( memberIsBanned() ) {
print $Message[MEMBER_BANNED()];
}[/perl]

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool


sangiro
Novice

Feb 24, 2002, 1:44 PM

Post #16 of 18 (5199 views)
Re: [yapp] Perl code (SSI) to detect day and time [In reply to] Can't Post

Good deal! Got it to work with one bug and one typo fix Smile:

if((WEDNESDAY <= $wday && $hour <= 9)
Should be:
if((WEDNESDAY >= $wday && $hour >= 9)

and

print "This is between wed 6:00 and thu 18:00\n";
Should read:
print "This is between wed 9:00 and thu 18:00\n";

Thanks a lot.

Safe swoops
Sangiro

http://www.dropzone.com/


freddo
User

Feb 24, 2002, 6:18 PM

Post #17 of 18 (5193 views)
Re: [mhx] Perl code (SSI) to detect day and time [In reply to] Can't Post

Hi MHX,

i didnt knew it would be optimized with an empty prototyping () and thanks a lot for the explanation, i'm playing with that stuff right now Smile

btw, i didnt forget about your ipv6 golf, but i had a lot of work this week and couldnt play with it before, but i'm coming with a nice solution (nice, sadly because it looks like nobody made it shorter yet)... your code for the IPv6 validation is helping a lot :-)

Read you soon,
Freddo
;---


yapp
User

Feb 25, 2002, 1:09 AM

Post #18 of 18 (5187 views)
Re: [sangiro] Perl code (SSI) to detect day and time [In reply to] Can't Post

Oeps! are you sure? Crazy Damn! Mad

Well, the hours are a mistake, but I thought you asked:

Quote
between 9pm on Wednesday and 6am on Thursday

That would imply: WED ... day ... THU
Shouldn't that mean that you should fill in a <= at the dots?
Then writing if(WED <= $day && $day <= THU){.. ???
Unless you ment between THU and WED (including the weekend) I'm confused.


But I think you got the idea of my code, and that you're able now to implement your own version.

Yet Another Perl Programmer

_________________________________
~~> [url=http://www.codingdomain.com]www.codingdomain.com <~~
More then 3500 X-Forum [url=http://www.codingdomain.com/cgi-perl/downloads/x-forum]Downloads! Cool

(This post was edited by yapp on Feb 25, 2002, 1:13 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives