LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-16-2006, 05:49 AM   #1
merana
Member
 
Registered: May 2002
Location: Philly/So. Jersey
Distribution: ESXi CentOS Red-Hat Ubuntuu Solaris Debian
Posts: 80

Rep: Reputation: 15
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...

Last edited by merana; 03-16-2006 at 06:22 AM.
 
Old 03-17-2006, 10:54 AM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 41
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing hash of hashes as a parameter (Perl) rose_bud4201 Programming 8 04-21-2005 08:18 PM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 11:22 AM
how to add -h parameter in script yulitao Programming 4 03-23-2004 08:30 PM
Perl parameter dvong3 Programming 3 02-21-2004 11:03 AM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 06:03 AM


All times are GMT -5. The time now is 03:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration