perl script for apache mod_rewrite

Discussion in 'Perl Misc' started by Aaron Haspel, Sep 8, 2006.

  1. Aaron Haspel

    Aaron Haspel Guest

    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
     
    Aaron Haspel, Sep 8, 2006
    #1
    1. Advertising

  2. Aaron Haspel

    Paul Lalli Guest

    Aaron Haspel wrote:
    > Greetings perl hackers:
    >
    > Not being a perl programmer myself,


    <snip>

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


    <snip>

    > 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
     
    Paul Lalli, Sep 8, 2006
    #2
    1. Advertising

  3. Aaron Haspel

    Guest

    I am wonder why dont u try using "my" variable instead of "local".
     
    , Sep 8, 2006
    #3
  4. Aaron Haspel

    Paul Lalli Guest

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


    I wonder why you think that would matter.

    Paul Lalli
     
    Paul Lalli, Sep 8, 2006
    #4
  5. wrote:

    > 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.
     
    Brian McCauley, Sep 8, 2006
    #5
  6. Aaron Haspel wrote:

    [ invalid code ]

    > Runs fine from the command line.


    So what does your actual code look like?
     
    Brian McCauley, Sep 8, 2006
    #6
  7. Aaron Haspel

    Aaron Haspel Guest

    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 McCauley wrote:
    > Aaron Haspel wrote:
    >
    > [ invalid code ]
    >
    > > Runs fine from the command line.

    >
    > So what does your actual code look like?
     
    Aaron Haspel, Sep 8, 2006
    #7
  8. Aaron Haspel

    Paul Lalli Guest

    Brian McCauley wrote:
    > 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
     
    Paul Lalli, Sep 8, 2006
    #8
  9. Aaron Haspel

    Paul Lalli Guest

    Paul Lalli wrote:
    > Brian McCauley wrote:
    > > 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
     
    Paul Lalli, Sep 8, 2006
    #9
  10. Aaron Haspel <> wrote:


    > $|=1; # Turn off buffering


    That does not turn off buffering you know.

    $| = 1; # Enable auto-flush


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Sep 9, 2006
    #10
  11. Aaron Haspel wrote:
    > 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
     
    Brian McCauley, Sep 9, 2006
    #11
  12. Aaron Haspel

    Aaron Haspel Guest

    Brian McCauley wrote:
    > 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
     
    Aaron Haspel, Sep 10, 2006
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Morris [Droopy eyes software]

    Apache mod_rewrite

    Peter Morris [Droopy eyes software], Feb 19, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    388
    Tampa.NET Koder
    Feb 19, 2005
  2. Alex Martinoff
    Replies:
    2
    Views:
    4,186
    Alex Martinoff
    Sep 7, 2003
  3. Replies:
    1
    Views:
    380
    Jukka K. Korpela
    Aug 5, 2006
  4. Samuel

    Detecting mod_rewrite via CGI

    Samuel, Nov 29, 2007, in forum: Python
    Replies:
    0
    Views:
    254
    Samuel
    Nov 29, 2007
  5. Patrick Joyce
    Replies:
    1
    Views:
    142
Loading...

Share This Page