What is the difference between the cd
shell command and the Perl function chdir
? Please can you explain with an example?
Asked
Active
Viewed 1.0k times
11

Jonathan Leffler
- 730,956
- 141
- 904
- 1,278

iDev
- 2,163
- 10
- 39
- 64
2 Answers
18
The cd
command changes the current directory of a shell process; the Perl chdir
function changes the current directory of a Perl process. They're exactly the same thing, just spelled differently.

Jonathan Leffler
- 730,956
- 141
- 904
- 1,278

Alan Curry
- 14,255
- 3
- 32
- 33
-
4Note that both shell `cd` and Perl `chdir` are implemented in terms of the `chdir(2)` system call. Modern shell `cd` commands do a good deal of futzing around to pretend that symlinks aren't, but when it is time to change directory, they still use `chdir(2)`. – Jonathan Leffler Aug 21 '12 at 01:09
-
1@JonathanLeffler I don't capitalize "perl" because the capitalization is based on the false assertion that "Perl the language" exists independently from "perl the program". In reality the language is whatever the (only) implementation accepts, even if the behavior contradicts the documentation. As they say in the perlfaq: "Q: Is there an ISO or ANSI certified version of Perl? A: Certainly not. Larry expects that he'll be certified before Perl is." I feel uneasy seeing something written in a way I definitely wouldn't have written it, with my name on it. – Alan Curry Aug 21 '12 at 08:47
-
@AlanCurry: I disagree with your style choice, but it's your post and you can do as you wish with it. – Jonathan Leffler Aug 21 '12 at 13:14
-
The choices are really "Perl" and "`perl`". "perl" makes no sense. – ikegami Aug 22 '12 at 15:13
2
Essentially both of them do the same thing, but chdir
is a POSIX system call while cd
is a normal function used in a program which in this case is the shell.
In practice, chdir
is called by cd
to make the change in directory since the program does not have kernel privileges to do it by itself.

Itamar Katz
- 21
- 1