When you put a variable in the first section of a s/// substitution, the variable's contents are used as a pattern, not as a simple string. So, c:\top is understood as 5 characters, with the middle character being a TAB ("\t"). You can fix this by using quotemeta.

I would also add a start-of-string anchor to be safer.

You may need something like make_path from File::Path instead of mkdir when creating multiple levels of directory all at once.

Working, tested exmaple code:

#!/usr/bin/perl use strict; use warnings; my $dir = 'c:\top\dir\file'; my $oldroot = 'c:\top'; my $newroot = 'c:\newtop'; my $newdir = $dir; print "Oldroot will be used as the pattern; It is '$oldroot'\n"; print "newdir was: '$newdir'\n"; $newdir =~ s/$oldroot/$newroot/ or warn "Can't find pattern '$oldroot' in dir string '$newdir'"; print "newdir now: '$newdir'\n\n"; my $oldroot_regex = quotemeta $oldroot; #$oldroot_regex = '^' . $oldroot_regex; # I would do this to be safe. print "Escaped pattern is '$oldroot_regex'\n"; print "newdir was: '$newdir'\n"; $newdir =~ s/$oldroot_regex/$newroot/ or warn "Can't find pattern '$oldroot_regex' in dir string '$newdi +r'"; print "newdir now: '$newdir'\n\n";
Output:
Oldroot will be used as the pattern; It is 'c:\top' newdir was: 'c:\top\dir\file' Can't find pattern 'c:\top' in dir string 'c:\top\dir\file' at pm_ +790602_03.pl line 13. newdir now: 'c:\top\dir\file' Escaped pattern is 'c\:\\top' newdir was: 'c:\top\dir\file' newdir now: 'c:\newtop\dir\file'
By the way, you will save yourself some future headaches if you just standardize on POSIX paths (forward slashes instead of backslashes), which *do* work fine in Perl on Win32. I use $path =~ tr{\\}{/} to fix up user input.


In reply to Re: replace part of directory by Util
in thread replace part of directory by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.