splitOnce has asked for the wisdom of the Perl Monks concerning the following question:

Hi , I have this guy
$path = RTE/hull/TR;
I want to add a new dir at the end of this path somthing like this :
$path = RTE/hull/TR/newDir;
I am doing somthing like this but don's seem to work:
$paht =~ s/$/\/newDir/g;
thanks

Replies are listed 'Best First'.
Re: Adding to an end of path
by sschneid (Deacon) on Jul 30, 2002 at 21:30 UTC
    How about just...
    $path = 'RTE/hull/TR'; $path .= '/newDir';
    No need for substitution.

    scott.
Re: Adding to an end of path
by thelenm (Vicar) on Jul 30, 2002 at 21:36 UTC
    The advice to use concatenation instead of substitution is good. That said, I can only see one reason why your code wouldn't work: you misspelled $path as $paht. Using strict will catch errors like that for you. Also, the /g regex modifier is unnecessary in this case. Update: Oops, two reasons. You should have quotes around "RTE/hull/TR" as well.

    -- Mike

    --
    just,my${.02}

      thank ya all
Re: Adding to an end of path
by jk2addict (Chaplain) on Jul 30, 2002 at 21:53 UTC

    Personally, I always use the File::Spec module, specifically the File::Spec->catfile method, for any path creation, tinkering.

    Anytime I try to do it myself, it always bites me in the butt for various reasons, spelling, unexpected input,etc. Plus, this way, the path could be more portable across file systems/platforms (should it ever be necessary) where the seperator is not "/".

    Just my $0.00002 worth. -=Chris

Re: Adding to an end of path
by silent11 (Vicar) on Jul 30, 2002 at 21:31 UTC
    Why not ...
    $path = 'RTE/hull/TR'; $new_dir = '/newDir'; $new_path = $path . $new_dir;


    -Silent11