Short answer, use chdir and your problem goes away. You're not doing error checking as perlstyle indicates, and that hides additional errors.

Here is a fuller explanation. The system command creates a new process that does something then exits. So if you have a system command that does a cd, the child process does a CD and goes bye bye. Which leaves the Perl process still in the original directory.

Actually it is a little worse than that. You're using both backticks and system. So you execute the command using backticks, and then the return from that (an empty string) is passed to system. Which then fails but you don't notice because you're not doing any error checking.

Anyways you need the changing directory to happen in the Perl process, and not in a temporary process that goes away. And that is why you have to use the built-in chdir instead.


In reply to Re: System Command - Change Dir by tilly
in thread System Command - Change Dir by ganilmohan

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.