I'm a little unclear as to what is wrong so I'll take a few stabs :)

Your code will successfuly change to ./mydirectory as long as the script is executed in the directory that contains ./mydirectory.

If you need to change to that directory from anywhere on the filesystem you just need to use a full path. i.e.

#!/usr/bin/perl use strict; my $dir = "/foo/bar/mydirectory"; chdir($dir) || die "Whoops: $!\n";

When the script exits you will still be in the directory you started in. The shell forks and runs your perl script, the script changes directory and exits. Nowhere in this process does the child process change the CWD of the parent. I don't actually believe that thisis possible.

It's kinda cheesy but if you want to run the script and be at the directory the script chdir'd to when it finishes you could run the chdir and then exec a shell. You would get a new shell to do with what you want. However, this would be a sub-shell of the one you started in.

Hope something in this rambling helps.


In reply to Re: How do I change directory to root directory by amw1
in thread How do I change directory to root directory by kdelph

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.