Edit in place (part2)
jzb
created: 2006-05-01 16:30:19
So, under 5.005_03 this works fine:
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?
Re: Edit in place (part2)
created: 2006-05-01 16:39:50
doesn't answer the "why" (may extra data point??), but does s/foo/$myvar/ie; work?
Re^2: Edit in place (part2)
jzb
created: 2006-05-01 16:58:57
No, however it's hanging before the regex. In a truss you see the somesub() and then it sleeps. I'm just grasping at straws, but I feel that it's about the ARGV. The <> is getting mucked somehow.
Re^3: Edit in place (part2)
created: 2006-05-01 17:03:52
Can you post the code that is in somesub()?
-imran
Re^4: Edit in place (part2)
jzb
created: 2006-05-01 17:13:04

It returns an IP address in the form of 1.2.3.4. I cannot post my code on a public forum (sorry).
-j

Re^5: Edit in place (part2)
created: 2006-05-01 17:20:54

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.


DWIM is Perl's answer to Gödel
Re^6: Edit in place (part2)
jzb
created: 2006-05-01 17:27:51
Yes, if the $myvar is given a string the problem goes away. I'm confused as it's only asking for an ip address to use to sub in a file with a placeholder.
Re^7: Edit in place (part2)
created: 2006-05-01 17:33:59

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.


DWIM is Perl's answer to Gödel
Re^5: Edit in place (part2)
created: 2006-05-01 17:32:02

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

Re^6: Edit in place (part2)
jzb
created: 2006-05-02 11:28:03
Ok here is a very simple script demonstrating the problem. If you comment out the input for hereistheip() and substitiute a static variable it works. This has to be due to the <>. Remember, this is to run under perl 5.005_03 only!
#!/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;
}
Re^7: Edit in place (part2)
created: 2006-05-02 11:32:17

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.

Re^8: Edit in place (part2)
jzb
created: 2006-05-02 11:38:49
Holy crap I feel stupid. I equated <> to be the same at .
Problem solved...
Re: Edit in place (part2)
created: 2006-05-01 16:40:07

Dunno, but you could try replacing
s/foo/$myvar/i;
with
substr($_, $-[0], $+[0] - $-[0], $myvar) if /foo/i;

Re: Edit in place (part2)
created: 2006-05-01 16:40:58
$myvar may have meta characters. See the quotemeta function, i.e. $myvar = quotemeta(somesub());
-imran
Re^2: Edit in place (part2)
created: 2006-05-01 17:24:51
No, $myvar can safely contain any character. You only need to escape text interpolated into the regexp, not the replace string.
Re: Edit in place (part2)
created: 2006-05-01 16:43:55

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.


DWIM is Perl's answer to Gödel

perlmonks.org content © perlmonks.org and Corion, CountOrlok, davidrw, GrandFather, ikegami, jzb

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03