sub changeme{
local $^I = ".$bak";
@ARGV = "somefile";
while (<>) {
s/foo/bar/i;
print;
}
}
However, substitute a variable in for bar:
sub changeme{
$myvar = somesub();
local $^I = ".$bak";
@ARGV = "somefile";
while (<>) {
s/foo/$myvar/i;
print;
}
}
And nothing happens - it just hangs. Why?
It returns an IP address in the form of 1.2.3.4.
I cannot post my code on a public forum (sorry).
-j
Seems to me that CountOrlok is on to something there. If you replace the sub with a string constant does the problem go away?
Perhaps you need to examine the contents of the sub. Presuming that that is where the problem is, try removing stuff from it until the problem goes away. If you can't find the issue that way, strip the code down to the point where you can show it to us and post it as a new SoPW.
Asking what? The user? Scraping a web site? Spelunking in a file or system setting? If you can't answer the question then you better sanitise your code and post it. Most likely though it's asking the user and the user doesn't deign to answer.
Then simplify the function until either 1) the error goes away (in which case you found the problem), or 2) the code is safe to post. How come you're not using Socket's inet_ntoa anyway? Example
#!/usr/bin/perl -w
use strict;
use diagnostics;
mysub();
sub mysub{
my $vari = hereistheip();
print "This is the IP you entered --> $vari\n";
local $^I = ".bak";
@ARGV = "stuff";
while (<>) {
s/FOO/$vari/i;
print;
}
}
sub hereistheip{
print "Enter a valid server IP \n --> ";
my $myip = <>;
chomp $myip;
return $myip;
}
You're reading from the console via the <> operator. The script will only continue to the next file (as given via @ARGV) once the current file has been completely read. If you want to continue that dangerous road, you'll have to type an EOF (^Z on Win32, ^D on Unixish operating systems) after you've entered the IP address.
Let me suggest ditching the in-place-edit magic and reimplementing the loop yourself. That way, you can do away with the diamond-operator/@ARGV magic and use explicit filehandles. Maybe it's already enough to change hereistheip to the following:
sub hereistheip {
print "Enter a valid server IP\n -->";
my $myip = ;
chomp $myip;
return $myip;
};
But I really think you're better off ditching the whole in-place approach and doing it yourself.
Dunno, but you could try replacing
s/foo/$myvar/i;
with
substr($_, $-[0], $+[0] - $-[0], $myvar) if /foo/i;
Seems most likely there is something external going on, like the file is accessed over a slow network or is mapping to the console in some fashion. On the face of it the code is fine.
perlmonks.org content © perlmonks.org and Corion, CountOrlok, davidrw, GrandFather, ikegami, jzb
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03