in reply to Search & Replace repeating characters

This has the smell of an X-Y Problem.

Can you explain the reasons for modifying the separators?

  • Comment on Re: Search & Replace repeating characters

Replies are listed 'Best First'.
Re^2: Search & Replace repeating characters
by Marshall (Canon) on Apr 22, 2020 at 23:59 UTC
    I agree with that. I mean C:\\temp is an invalid path under Windows. It would be better to fix the thing that generates this invalid path rather than trying to hack that path into a valid path. The crux of an X-Y problem is that the OP is asking the wrong question or that the question is not well specified.
      "I mean C:\\temp is an invalid path under Windows."

      While I'm not disagreeing with that absolute statement, C:\\temp may be required under a Windows environment. I came across an example of this fairly recently.

      Consider a pp command on cmd.exe something like this (using Strawberry Perl):

      C:\some\dir>pp @long_list_of_options.txt -o filename.exe filename.pl

      Options (in long_list_of_options.txt) which include paths cannot be written like this for example:

      --link=C:\path\to\some.dll

      The parts like \X will be interpreted as backslashed escapes: resulting in just X, or special characters (e.g. newline, tab, and so on). The example option I showed would end up like:

      --link=C:path osome.dll

      In order to get it to resolve to the example shown, you'd need to write:

      --link=C:\\path\\to\\some.dll

      That doesn't mean that an XY Problem doesn't exist; it just shows that it might not exist. Having said that, the terminal \\\\ certainly looks highly dodgy.

      — Ken

Re^2: Search & Replace repeating characters
by g_speran (Scribe) on Apr 23, 2020 at 12:45 UTC
    not an X-Y Problem. So that I can properly give an example of what I was trying to accomplish, I just assigned c:\\temp\\\\ to the variable $ScriptDir. in actuality, $ScriptDir gets its value from the terminal input from the end user $ScriptDir=<STDIN>; So I am trying to plan for the end user entering slashes and correct it

      I think you need to make your solution either smarter or not as helpfull, because Windows file paths are allowed to start with \\ in UNC notation. For example, this is a valid UNC filepath:

      \\servername\c$\foo.txt
      Ok. Now I understand this better.
      Use the command prompting loop to reject invalid syntax of the input.
      Below I reject input lines that have a double // or \\.
      use strict; use warnings; my $dir; while ( (print "enter directory; "), $dir=<STDIN> and $dir =~ /\\\\|\/ +\//g) { print "invalid directory syntax... double ", '// or \\\\', " not al +lowed!\n"; } $dir =~ s/^\s*|\s*$//g; #remove leading and trailing spaces print "INPUT LOOP says $dir\n"; __END__ C:\Monks>perl CommandLoopNoDoubleSlash.pl enter directory; C:\\x invalid directory syntax... double // or \\ not allowed! enter directory; D:\x\temp// invalid directory syntax... double // or \\ not allowed! enter directory; C://x invalid directory syntax... double // or \\ not allowed! enter directory; C:/X INPUT LOOP says C:/X
      Now it is also possible perhaps have a sub that prompts for a valid directory and that sub continues to loop until a valid directory is entered. I would return a file handle from that sub. In that sort of case, the sub tries to open the dir name that the user entered and it works or it doesn't. Loop until the openddir() works. Remember to allow for leading and trailing whitespace for all command line entries.