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

Hi all ! I can't figure out what is the problem here. I'm trying to do 'sed' like regex replace to files, but, at the same time ask a question if I want it run for the file in question. The problem is that the second time I call the ja_nei() it seems to hang. My guess is that there is some buffer issues that I can't seem to figure out. I've used to run the same code but by calling sed from doSystemCommand - but, I hate having to use sed for something that I should be able to do in perl only. I was hoping to write this without any use of modules.
print "Process \"$change_me\" ? (j/n) : "; $svar = ja_nei(<>); if ($svar) { fixer( 'anonymous_enable=YES','anonymous_enable=NO',"$change_me" ); fixer( '#nopriv_user=ftpsecure','nopriv_user=ftpsecure',"$change_me" + ); doSystemCommand("useradd -m ftpsecure -s /bin/bash"); } ## # Subs ## sub ja_nei { #chomp( my $input = (<STDIN>) ); my $input = (<STDIN>); if ( $input =~ /^j|^y/i ) { return 1; } else { return 0; } } sub fixer { my $pat = shift; my $to = shift; my $file = shift; my $count = 0; local $^I = ''; undef @ARGV; push @ARGV, $file; while (<>) { if (s/$pat/$to/g) { $count++; } print; } if ( $count == 0 ) { say "WARNING : No changes made to file \"$file\""; } elsif ( ($g_debug) && ($count) ) { say "\"$pat\" changed to \"$to\" in \"$file\""; } } sub doSystemCommand { my ($systemCommand) = @_; my $returnCode = system($systemCommand ); if ( $returnCode != 0 ) { die "Failed executing [$systemCommand] : $!\n"; } return; }

Replies are listed 'Best First'.
Re: inline replace and stdin problem
by choroba (Cardinal) on May 21, 2014 at 12:02 UTC
    Why do you pass <> to ja_nei as a parameter, when you never touch the parameter in its body, but read from STDIN instead?
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: inline replace and stdin problem
by Anonymous Monk on May 21, 2014 at 12:00 UTC
Re: inline replace and stdin problem
by Thalamus (Acolyte) on May 21, 2014 at 12:21 UTC
    Thank you. You both pointed me to the error. I was only lucky that I did a cut of where the culprit where located. Added by a fellow collegue of mine and I didn't see it. The advice to rewrite - thanks for that to !