in reply to Re^3: Script won't run
in thread Script wont run

I apologize,after analyzing I realized that the file would be updated after the while loop ends , I was pressing CTRL + C to see if the file is being written to correctly. I do have other questions if you can please bear with me:
s{Password=\Q$password\E}{Password=$newpass} if $answer =~ /y/i;
wrapping the old password with \Q\E means that it will remain unchanged evidenced by : The sentence remain unchanged, so why do \q\e instead of just doing s{$password}{$newpass}
my $sentence = 'The quick brown fox jumped over the lazy dog'; my $substring = 'quick.*?fox'; $sentence =~ s{\Q$substring\E}{big bad wolf};
The script I ended working with is , do you have any recommendations or critque, and also can you suggest how I could get it to update immediately to the file instead of waiting till the loop ends?
#!/usr/bin/perl use strict; use warnings; use Tie::File; my $filename = '/home/stain/Downloads/REMOTEopen.xml'; tie my @array, 'Tie::File', $filename || die "Cannot tie file $filenam +e to array: $!"; foreach my $line (@array) { my $newline = $line; # Make a copy 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 = <>); # Change the copy $newline =~ s{$password}{$newpass} if $answer =~ /y/i; } $line = $newline; # Update this line in the file } untie @array or die "Cannot untie file '$filename': $!";

Replies are listed 'Best First'.
Re^5: Script won't run
by Athanasius (Archbishop) on Nov 25, 2015 at 08:15 UTC

    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,