perl script for apache mod_rewrite

A

Aaron Haspel

Greetings perl hackers:

Not being a perl programmer myself, I'm trying to get a simple perl
script to work for url rewriting and I'm having a bit of trouble. My
script looks like this:

#!/usr/bin/perl
$|=1; # Turn off buffering
while ( <STDIN> )
{
local $postid = $_;
++postid;
print "/?p=$postid\n";
}

Simple enough. Just takes old urls, converts them to numbers, adds 1,
and returns a string. Runs fine from the command line. But when I try
to use it in a RewriteRule in apache, it behaves strangely. The first
time it executes, it returns null. The next time it executes, it
returns what one would have expected from the first run, the third time
from the second, and so forth. So, for example, if I run it three times
I get the following:

arg: /archives/00000590.html return: null
arg: /archives/00000577.html return: /?p=591
arg: /archives/00000490.html return: /?p=578

I thought about posting this to apache, but the script is executing
when it's supposed to, just weirdly, so I thought it was more of a perl
problem. Any help is most appreciated.

Regards,
Aaron Haspel
 
P

Paul Lalli

Aaron said:
Greetings perl hackers:

Not being a perl programmer myself,

Runs fine from the command line. But when I try
to use it in a RewriteRule in apache, it behaves strangely.

I thought about posting this to apache, but the script is executing
when it's supposed to, just weirdly, so I thought it was more of a perl
problem.

Think again, I'd suggest. You've already proven that when fed correct
data, it works as it's supposed to. Therefore, the Perl is fine. The
input, however, may not be. Talk to the Apache people about how the
data is being fed to the program.

Paul Lalli
 
B

Brian McCauley

I am wonder why dont u try using "my" variable instead of "local".

I wonder why you think this is relevant.

Granted there's no reason for the OP to be using local() where he could
use my() but this will not account for his problem.
 
A

Aaron Haspel

Copied directly, here it is:

#!/usr/bin/perl
$|=1; # Turn off buffering
while (<STDIN>)
{
local $postid = $_;
++$postid;
print "/?p=$postid\n";
}

I apologize if the previous listing was wrong, and also for the bad
style that other commenters pointed out.

AH


Brian said:
Aaron Haspel wrote:

[ invalid code ]
Runs fine from the command line.

So what does your actual code look like?
 
P

Paul Lalli

Brian said:
Aaron Haspel wrote:

[ invalid code ]
Runs fine from the command line.

So what does your actual code look like?

I may be missing something, but would you care to explain what was
"invalid" about the code the OP posted?

Paul Lalli
 
P

Paul Lalli

Paul said:
Brian said:
Aaron Haspel wrote:

[ invalid code ]
Runs fine from the command line.

So what does your actual code look like?

I may be missing something, but would you care to explain what was
"invalid" about the code the OP posted?

D'oh. And now I see it. Missing sigil. Sorry, Brian, I should have
attempted to run it before asserting it was not invalid....

Paul Lalli
 
B

Brian McCauley

Aaron said:
Copied directly, here it is:

#!/usr/bin/perl
$|=1; # Turn off buffering
while (<STDIN>)
{
local $postid = $_;
++$postid;
print "/?p=$postid\n";
}

Thanks. Unfortunately there's nothing there to account for why there
appears to be an extra newline emitted at the start.

But just too be totally sure...

echo 123 | myscript | od -c
 
A

Aaron Haspel

Brian said:
Thanks. Unfortunately there's nothing there to account for why there
appears to be an extra newline emitted at the start.

But just too be totally sure...

echo 123 | myscript | od -c

Turned out, as Paul originally surmised, to be an apache issue. There
was a known sync bug in mod_rewrite in the version I was running, and
upgrading made it disappear.

Thanks for looking at my awful Perl anyway.

Regards,
AH
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top