Perl

Moderators: Jonathan
Number of threads: 1236
Number of posts: 3605

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Shell Posted by generalen on 20 Oct 2004 at 2:17 AM
Hi again!!

#!/usr/bin/perl -w

print $ENV{'HOMEPATH'};
print ">";
chomp($newdir = <stdin>);
chdir($newdir) ||
die "cannot chdir to $newdir: $!";
opendir(DOT, ".") ||
die "cannot open dir . $!";
foreach (sort readdir(DOT)) {
print "$_\n";
}
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 127.0.0.1'); My last answer I get whas this but it dident tell me much - Iam pretty new to this as you understand now

HELP :(



Report
Re: Shell Posted by Weirdofreak on 20 Oct 2004 at 2:32 PM
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, system("dir") 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, print `dir` is the same as system('dir'), but slower, and my $dir = `dir`; print $dir 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 system(); $foo = ``.)



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.