LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Testing parameter input to perl script (https://www.linuxquestions.org/questions/programming-9/testing-parameter-input-to-perl-script-425322/)

merana 03-16-2006 04:49 AM

Testing parameter input to perl script
 
Hi All,

I'm working on trying to get a redirector script to add to a squid install and I am trying to debug the sample script but I am having a perl brain deficiency...

The script is called Redirector.pl and is as follows:

Code:

#!/usr/bin/perl
#
#
#
$|=1;

while (<>) {
 @X = split;
 $url = $X[0];
 if ($url =~ /^http:\/\/.*/) {
  $url =~ s/^http/https/;
  print "302:$url\n";
 } else {
  print "403:$url\n";
 }
}

So right off I see that $| is taking stdin and using it as the value to operate on right? So when @X=split; runs it is using whatever came in on stdin...

So to test this supposition (suppository? LOL) in the shell; it would be like:

Code:

echo http://some.url.info | ./Redirector.pl
I try this but I get:

Code:

./Redirector.pl: line 1: http://some.url.info: No such file or directory
How should I format the shell command to process properly? I need to maintain the pipe input as this is how squid will pass the data into the script... Also am I correct in assuming that the prints will just go out on stdout?

Your comments appreciated!

: EDIT :

I just added the "-w" to the "#!/usr/bin/perl" line and now I am getting the output to the screen when I run:

Code:

echo http://some.url.info | ./Redirector.pl
I now get:

Code:

302:http://some.url.info
Now I just have to figure out if that will break the way it works in squid...

taylor_venable 03-17-2006 09:54 AM

The problem is that $| doesn't change the default filehandle. It's the autoflush option; setting it to non-zero forces a buffer flush after every write operation (see `man perlvar`). It's best to be explicit with the diamond operator:
Code:

#!/usr/bin/env perl

use warnings;
use strict;

while (<STDIN>) {
    my @X = split;
    my $url = $X[0];
    if ($url =~ m{^http://.*}) {
        $url =~ s{^http}{https};
        print "302:$url\n";
    } else {
        print "403:$url\n";
    }
}

This works fine for me.


All times are GMT -5. The time now is 06:40 PM.