Hello again cbtshare,
wrapping the old password with \Q\E means that it will remain unchanged ..., so why do \q\e instead of just doing s{$password}{$newpass}
The purpose of adding \Q to a regex is to prevent any “special” regex characters from having their special meanings. For example, if the data contains:
Password="abcde$"
then without a \Q the $ will be interpreted to mean match the end of a line, which will cause the match to fail (unless abcde actually happens to come immediately before the end of a line, followed immediately by " at the beginning of the next line). But when preceded by a \Q, special characters like $, ., +, and * lose their special regex meanings and are treated as literal characters. That’s why, if there’s any possibility that a password may contain a character which the Perl regex engine considers to be “special,” it’s good practice to add \Q ... \E around the variable containing that password.
can you suggest how I could get it to update immediately to the file instead of waiting till the loop ends?
That depends on which loop you have in mind. As I explained above, the file can’t be updated within the inner (while) loop. However, the file is already being updated between each iteration of the outer (foreach) loop. You can see this by adding another <> as follows:
use strict; use warnings; use Tie::File; $| = 1; my $filename = 'REMOTEopen.xml'; tie my @array, 'Tie::File', $filename or die "Cannot tie file $filename to array: $!"; for my $line (@array) { my $newline = $line; while ($line =~ / < Node \s* Name="(.*?)" .*? Password="(.*?)" .*? + > /mgx) { my $name = $1; my $password = $2; print "Enter the plain text password for $name, ", "password is currently $password\n"; chomp(my $newpass = <>); print "You will now swap $password for $newpass\n"; print "Continue? (y/n)\n"; chomp(my $answer = <>); $newline =~ s{\Q$password\E}{$newpass} if $answer =~ /y/i; } $line = $newline; print "Press <Enter> to continue..."; <>; } untie @array or die "Cannot untie file '$filename': $!";
Assuming a file named “REMOTEopen.xml” is present in the current directory and contains data like this:
<Node Name="MUN" Type="Connection" Descr="" Icon="mRemoteNG" Panel="Ge +neral" Username="root" Domain="" Password="a/5pqLSsMnvRfylxdN6coiFTdc +2+$"> </Node> <Node Name="ubunugit" Type="Connection" Descr="" Icon="mRemoteNG" Pane +l="General" Username="root" Domain="" Password="bBxKPKXIhw1wKEV9aEUN1 +yynoRGOWT$"> </Node>
you can check the contents of the data file each time Press <Enter> to continue... is printed to the screen, and you should see that the file has indeed been updated as expected.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re^5: Script won't run
by Athanasius
in thread Script wont run
by cbtshare
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |