Re: read an integer from a file
by cdarke (Prior) on Jun 15, 2010 at 11:29 UTC
|
use strict;
use warnings;
Please always check that you can open the file. Then you should chomp:open (MYFILE, "status2.txt") or die "Unable to open status2.txt: $!";
my $a = <MYFILE>;
chomp $a;
| [reply] [d/l] [select] |
|
|
my $a = "0\n";
print "is zero\n" if $a == 0;
| [reply] [d/l] [select] |
Re: read an integer from a file
by Boldra (Curate) on Jun 15, 2010 at 12:19 UTC
|
Try changing the print $a to print STDERR $a and looking in the log file.
As it's written it looks like $a is going to be the first thing sent back to the browser, making further headers useless.
Update
And use IO::All, it's just so cool.
open (MYFILE, "status2.txt");
$a=<MYFILE>;
print "$a";
# can be written as
use IO::All;
$a < io("status2.txt");
print "$a";
| [reply] [d/l] |
Re: read an integer from a file
by ahmad (Hermit) on Jun 16, 2010 at 01:28 UTC
|
What's your problem ? "Page not being redirected" ? if so, Then it's because you're printing an invalid header.
Your header should look something like:
print "Status: 301\n";
print "Location: $_\n\n";
You may want to use CGI module, it will help to do things faster.
| [reply] [d/l] |
|
|
What you means by invalid header?
actually this script is make for squid. i want to do an URL redirection with squid. the redirection will based on the condition of the network. if the newtork is down,squid will redirect the URL to a fix URL(as in the code). Could you advised me on how to make it works.
| [reply] |
|
|
Well I'm not sure about squid, but you should break this into two parts:
- check you are getting the behaviour you expect WITHOUT squid,
- integrate with squid.
You might not get as much help with part 2 from Perlmonks as you would like, but you will get more help with part 1 if you're clear about what's going wrong.
Since you asked again in the chatterbox about "problem reading a file", I advise you again to look at IO::All. If you're lucky, you can install it just by typing "cpan IO::All". If it turns out to be a bit more complicated, it's definitely worth at least one whole week of effort to get cpan working properly. If you can't install modules from cpan, you are missing out on most of the power of Perl, and you may even find people here getting frustrated with you. The particulars of installing cpan modules vary from site to site, so if you have problems, it's a good idea to talk to other people using Perl at your location.
| [reply] |
|
|
|
|
|
Re: read an integer from a file
by Khen1950fx (Canon) on Jun 16, 2010 at 09:36 UTC
|
#!/usr/bin/perl
use strict;
use warnings;
$| = 1;
while ( defined( $_ = <ARGV> ) ) {
my @X = split( " ", $_, 0 );
my $url = $X[0];
open( MYFILE, 'status2.txt' )
or die "Couldn't open file!";
my $a = <MYFILE>;
close(MYFILE);
print "$a\n";
if ( $a == 0 ) {
if ( not $url =~ m[^http://123.1.34.107:8080] ) {
$_ = $url;
s[^http://(.*)(.*)][http://123.1.34.107:8080];
print "301:$_\n";
}
else {
print "$url\n";
}
}
}
| [reply] [d/l] |
|
|
thanks for all especially to Boldra and Khen1950fx,
i'm able to get the right result by changing this code from
s/^http:\/\/(.*)\/(.*)/http:\/\/123.1.34.107:8080\/Web/;
to s[^http://(.*)(.*)][http://123.1.34.107:8080];
May i know what is the difference with above code?
as i manually test it for the 1st code,it seem to output the same input when i enter the input.
and for 2nd code, i will get the right output=123.1.34.107
when i enter any input.
| [reply] [d/l] [select] |
|
|
The former adds /Web, the latter doesn't
| [reply] |
|
|
|
|
|