in reply to perl command line prompts
Turns out it was your $/ that caused it. Btw, is there supposed to be only 1 change in the entire file?use strict; use warnings; my $original = &promptUser("Enter original filename"); my $modified = &promptUser("Enter modified filename"); open(my $ORIG, "$original") or die "could not open $original"; binmode($ORIG); open(my $MOD, ">$modified") or die "could not open $modified"; binmode($MOD); # commented out $/ = "\x00"; while (<$ORIG>) { my $count = ((my $change = $_) =~ s/(\x33\x33\x39\x39.*?\x00)/"\x00" + x length($1)/e); if ($count == 0) { print $MOD $_; } else { my $answer = &promptUser("Modify $1?"); if ($answer eq "y") # <-- eq for test, == is for numerical compari +sons { print $MOD $change; } else { print $MOD $_; } } } close $MOD; close $ORIG; sub promptUser { my($promptString, $defaultValue) = @_; if ($defaultValue) { print $promptString, "[", $defaultValue, "]: "; } else { print $promptString, ": "; } $| = 1; # force a flush after print my $line = <STDIN>; # get the input from STDIN chomp($line); if ($defaultValue) { return $line ? $line : $defaultValue; } # return $line if it ha +s a value else { return $line; } }
Oh, and please, in the future, for the sake of your own sanity as well as ours:
- use strict;
- use warnings;
- indent
- just like in english(or other languages), a space after a comma please
- (personal opinion) assign $_ to a named variable if you're going to use that value for more than the most basic of functions.
Remember rule one...
|
|---|