ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a line in a perl script, that replaces urls with an other one:
s@^(http:[^ ]*) @http://192.168.0.3/httpgetfile3.php?url=\1 @;
Could it be modified so that instead of simply substituting backreference \1, it substitutes the urlencoded backreference \1?
This is syntactically incorrect for sure, but it may help to make clear what I would like to do:
s@^(http:[^ ]*) @http://192.168.0.3/httpgetfile3.php?url=urlencode(\1) @;
@estabroo I had the same idea as yours. It's just too bad I didn't know how to specify the results using a variable inside the replacement expression. I've tried $1 = uri_escape($^N) but it was a wrong idea since $1 is readonly. I thought that ${var} is only evaluated before the expression starts runtime.
My example now is this:
Code:
#!/usr/bin/env perl
use URI::Escape;
use strict;
our $var;
while (<>) {
s@(http://[^[:blank:]'"]+)(?{ $var = uri_escape($^N); })@http://192.168.0.3/httpgetfile3.php?url=${var}@g;
print $_;
}
Code:
perl script.pl < file
Edit: I think this can be easily converted to a one-liner.
Last edited by konsolebox; 08-17-2010 at 11:05 PM.
Anyway, the script is going to be an url redirector for squid, so:
a) surplus part of the line (following the space) can be junked (or squid junks it),
b) it need not necessarily be a one-liner. I only try this one-liner, because I have no perl knowledge, and this one-liner is simple and works so far, whilst I remember that the multi-liners I have tried some years ago when I was faced with a similar task, only worked from the command line, but stalled squid for some reason.
The script is going to be part of a simple filtering proxy for my handheld, and its only purpose is to convert/scale down media files in order that they can be viewed on a weaker hardware and lower bandwidth. (However, all urls are passed to the filtering php script, since I want to filter based on response mime-type, rather than request url.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.