CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
system("cd $dir") does not find existing directory

 



michez
New User

Aug 13, 2009, 6:10 AM

Post #1 of 3 (2681 views)
system("cd $dir") does not find existing directory Can't Post

Hi,

I am running the following test case in a shell:
----------------------------
#!/usr/bin/perl -w
use strict;
my $dir = "./pgiza";
if(!-e $dir) {
system("mkdir $dir");
}
system("cd $dir");
----------------------------
the mkdir command works fine, but the cd command outputs:
Can't exec "cd": No such file or directory at ../test_case.pl line 7.

Could you please explain why it happens, and how can I make it work ?


FishMonger
Veteran / Moderator

Aug 13, 2009, 6:34 AM

Post #2 of 3 (2680 views)
Re: [michez] system("cd $dir") does not find existing directory [In reply to] Can't Post

Why are you using the system calls when Perl has its own built-in functions.


Quote
C:\>perldoc -f mkdir
mkdir FILENAME,MASK
mkdir FILENAME
mkdir Creates the directory specified by FILENAME, with permissions
specified by MASK (as modified by "umask"). If it succeeds it
returns true, otherwise it returns false and sets $! (errno). If
omitted, MASK defaults to 0777. If omitted, FILENAME defaults to
$_.

In general, it is better to create directories with permissive
MASK, and let the user modify that with their "umask", than it
is to supply a restrictive MASK and give the user no way to be
more permissive. The exceptions to this rule are when the file
or directory should be kept private (mail files, for instance).
The perlfunc(1) entry on "umask" discusses the choice of MASK in
more detail.

Note that according to the POSIX 1003.1-1996 the FILENAME may
have any number of trailing slashes. Some operating and
filesystems do not get this right, so Perl automatically removes
all trailing slashes to keep everyone happy.

In order to recursively create a directory structure look at the
"mkpath" function of the File::Path module.



Quote
C:\>perldoc -f chdir
chdir EXPR
chdir FILEHANDLE
chdir DIRHANDLE
chdir Changes the working directory to EXPR, if possible. If EXPR is
omitted, changes to the directory specified by $ENV{HOME}, if
set; if not, changes to the directory specified by $ENV{LOGDIR}.
(Under VMS, the variable $ENV{SYS$LOGIN} is also checked, and
used if it is set.) If neither is set, "chdir" does nothing. It
returns true upon success, false otherwise. See the example
under "die".

On systems that support fchdir, you might pass a file handle or
directory handle as argument. On systems that don't support
fchdir, passing handles produces a fatal error at run time.



FishMonger
Veteran / Moderator

Aug 13, 2009, 6:36 AM

Post #3 of 3 (2678 views)
Re: [michez] system("cd $dir") does not find existing directory [In reply to] Can't Post

If you need to make or remove a directory tree, then look at File::Path.

http://search.cpan.org/~dland/File-Path-2.07/Path.pm

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives