Shell - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.


generalengeneralen Posts: 68Member
Hi again!!

#!/usr/bin/perl -w

print $ENV{'HOMEPATH'};
print ">";
chomp($newdir = );
chdir($newdir) ||
die "cannot chdir to $newdir: $!";
opendir(DOT, ".") ||
die "cannot open dir . $!";
foreach (sort readdir(DOT)) {
print "$_
closedir (DOT);

I have written this code where the program starts in my homecatalog..

Say I want to execute a program under..say C:Program...what do I write in my code then?

system('ping'); My last answer I get whas this but it dident tell me much - Iam pretty new to this as you understand now



  • WeirdofreakWeirdofreak Posts: 439Member
    The system() function (well, syscall) runs a shell command from the cwd of the perl script (which is the directory it's being run from, not where it is on the hard drive) and prints the output. It returns the exit status, which is useful if you want to check for failure. Therefore, [grey]system("dir")[/grey] will print the contents of your current directory.

    You can also use backticks (or the qx construct), which give the output to be stored in a variable. Thus, [grey]print `dir`[/grey] is the same as [grey]system('dir')[/grey], but slower, and [grey]my $dir = `dir`; print $dir[/grey] is the same again but even slower and more wasteful of resources (unless you need to do something with the output as well as print it, in which case it would be faster and more reliable than doing [grey]system(); $foo = ``[/grey].)
Sign In or Register to comment.