Perl substitution working on some machines but not others?

Discussion in 'Perl Misc' started by therocket79@yahoo.co.uk, Nov 21, 2006.

  1. Guest

    Hi,

    I have a question about the following line of code:

    $new_file =~ s/^$from_dir/$to_dir/;

    I have 2 variables $from_dir and $to_dir which are different directory
    path names. I also have the $new_file variable which is the full path
    of a file. I am trying to substitute the first part of the file path
    ($from_dir) with a new file path ($to_dir), following on from this I
    copy the original file to the new location.

    An example:

    $new_file = C:/directory1/file.txt
    $from_dir = C:/directory1
    $to_dir = C:/directory2

    I would like the result of the substitution to be $new_file =
    C:/directory2/file.txt, so I can then copy C:/directory1/file.txt to
    C:/directory2/file.txt

    However, this seems to work on some machines but fail on others. The
    reason being the substitution doesn't seem to take place correctly and
    I end up trying to copy something to the same location. Can anyone
    help with why this is happening?
    , Nov 21, 2006
    #1
    1. Advertising

  2. -berlin.de Guest

    <> wrote in comp.lang.perl.misc:
    > Hi,
    >
    > I have a question about the following line of code:
    >
    > $new_file =~ s/^$from_dir/$to_dir/;
    >
    > I have 2 variables $from_dir and $to_dir which are different directory
    > path names. I also have the $new_file variable which is the full path
    > of a file. I am trying to substitute the first part of the file path
    > ($from_dir) with a new file path ($to_dir), following on from this I
    > copy the original file to the new location.
    >
    > An example:
    >
    > $new_file = C:/directory1/file.txt
    > $from_dir = C:/directory1
    > $to_dir = C:/directory2
    >
    > I would like the result of the substitution to be $new_file =
    > C:/directory2/file.txt, so I can then copy C:/directory1/file.txt to
    > C:/directory2/file.txt
    >
    > However, this seems to work on some machines but fail on others. The
    > reason being the substitution doesn't seem to take place correctly and
    > I end up trying to copy something to the same location. Can anyone
    > help with why this is happening?


    Too little info. What is the exact code that does (or doesn't do) the
    substitution? What is the content of $from_dir, $to_dir and $new_file
    in a case where it works and in a case where it doesn't? Also name the
    OS and its version for a working case and one that doesn't work.

    Anno
    -berlin.de, Nov 21, 2006
    #2
    1. Advertising

  3. Guest

    Hi,

    Thanks for your response. I've managed to get it wokring now, it was
    actually to do with forward and backslashes being included in file
    paths inconsistently.

    Thanks again.

    -berlin.de wrote:

    > <> wrote in comp.lang.perl.misc:
    > > Hi,
    > >
    > > I have a question about the following line of code:
    > >
    > > $new_file =~ s/^$from_dir/$to_dir/;
    > >
    > > I have 2 variables $from_dir and $to_dir which are different directory
    > > path names. I also have the $new_file variable which is the full path
    > > of a file. I am trying to substitute the first part of the file path
    > > ($from_dir) with a new file path ($to_dir), following on from this I
    > > copy the original file to the new location.
    > >
    > > An example:
    > >
    > > $new_file = C:/directory1/file.txt
    > > $from_dir = C:/directory1
    > > $to_dir = C:/directory2
    > >
    > > I would like the result of the substitution to be $new_file =
    > > C:/directory2/file.txt, so I can then copy C:/directory1/file.txt to
    > > C:/directory2/file.txt
    > >
    > > However, this seems to work on some machines but fail on others. The
    > > reason being the substitution doesn't seem to take place correctly and
    > > I end up trying to copy something to the same location. Can anyone
    > > help with why this is happening?

    >
    > Too little info. What is the exact code that does (or doesn't do) the
    > substitution? What is the content of $from_dir, $to_dir and $new_file
    > in a case where it works and in a case where it doesn't? Also name the
    > OS and its version for a working case and one that doesn't work.
    >
    > Anno
    , Nov 21, 2006
    #3
  4. Dr.Ruud Guest

    schreef:

    > $new_file =~ s/^$from_dir/$to_dir/;


    $new_file =~ s/^\Q$from_dir/$to_dir/;

    See perldoc -f quotemeta.

    --
    Affijn, Ruud

    "Gewoon is een tijger."
    Dr.Ruud, Nov 22, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Spiros Bousbouras
    Replies:
    9
    Views:
    342
    David T. Ashley
    Dec 19, 2006
  2. thatsMaBoy
    Replies:
    5
    Views:
    427
    thatsMaBoy
    Mar 13, 2007
  3. Zak
    Replies:
    3
    Views:
    319
    James Kanze
    Sep 17, 2008
  4. R. G.
    Replies:
    2
    Views:
    172
    R. G.
    Jul 8, 2004
  5. Travis
    Replies:
    3
    Views:
    352
Loading...

Share This Page