Q:

Write a Perl script that lists the directory contents of a directory specified by the user

I have tried this code but it doesnt work for the user-input directory.
It only lists the PWD.
Help!
#script to list the directory contents of a user specified directory

system("pwd");
print "enter the path of your d1rectory\n";
$path =<STDIN>;
system(" cd Spath");
#system ("chdir $path");
system("ls");

A:

A Unix process has something called an "environment" associated with it. The environment contains details of how the process is being run. One of the elements of the environment, for example, is the current working directory.
When a process starts another process, the new, child process inherits a copy of the environment from the parent process. The child process can change anything it wants in its copy of the environment, but it cannot change the parent's copy of the environment (or, at least, not easily).
Your code involves four processes. In call to system() creates a new process, with a new copy of the environment. When each call to system() exits, its environment ceases to exist.

Your program is the first process. It inherits a copy of the environment of the shell process that starts it. This environment has a current working directory.
The first call to system() creates a new process and gives it a copy of the main program's environment. That subprocess runs pwd and exits. Its copy of the environment disappears.
The next call to system() creates another new process and gives it a new copy of the main program's environment. That subprocess changes its current working directory - but only for its copy of the environment. When the call to system() exits, that copy of the environment (with its changed current working directory) ceases to exist).
The final call to system() creates another new process and gives it a new copy of the main program's environment (which still has the original program's current working directory). That process calls ls on its current directory and then exits - removing its environment in the process.

So, effectively, your call to cd does nothing as it changes the directory in an environment that immediately ceases to exist.
The quick fix (as you have been shown) is to just pass the $path variable to ls.
The correct fix (as you have also been shown) is to not use subprocesses to do this and to use, instead, Perl's built-in tools.
When I see questions like this, I have to wonder what they are teaching on computing courses these days :-)

A:

Already @toolic mentioned in a comment you have to make system "ls $path"
But you are already in Perl, you can use you the Perl Inbuilt functions like glob or opendir. for example
my $s = <STDIN>;
chomp $s;
$s .= '/' if length($s);
while(my $file = glob("\Q$s\E*")){

    print $file,"\n";
}

