long has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, i have setup a perl script that will read a text file and then do a URL redirection. if the value of the file is '0',the current webpage will be redirect to a fix webpage.but if its value not equal to '0' it will do nothing. my problem is i cannot get the desired result even the value of the text file changed. below is my code.
#!/usr/bin/perl $|=1; while (<>) { @X = split; $url = $X[0]; open (MYFILE, "status2.txt"); $a=<MYFILE>; print "$a\n"; if( $a == 0) { if ($url !~ /^http:\/\/123.1.34.107.8080\/Web/) { $_ = $url; s/^http:\/\/(.*)\/(.*)/http:\/\/123.1.34.107:8080\/Web +/; print "301:$_\n"; } else { print "$url\n"; } } else { print "$url\n"; } }
the output i get from the text file when i read it didn't match witht the condition. it will be always get into the first loop even the value of the status.txt changed. Please do advised me..thanks

Replies are listed 'Best First'.
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;
      Then you should chomp

      == should work without chomping (while $a eq "0" would not):

      my $a = "0\n"; print "is zero\n" if $a == 0;
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";


    - Boldra
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.

      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.
        Well I'm not sure about squid, but you should break this into two parts:
        1. check you are getting the behaviour you expect WITHOUT squid,
        2. 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.



        - Boldra
Re: read an integer from a file
by Khen1950fx (Canon) on Jun 16, 2010 at 09:36 UTC
    Maybe this'll work:
    #!/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"; } } }
      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.
        The former adds /Web, the latter doesn't