Berkeley CSUA MOTD:Entry 22559
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/04/06 [General] UID:1000 Activity:popular
4/6     

2001/9/20 [Computer/SW/Languages/Perl] UID:22559 Activity:nil
9/20    For a long time i have been doing silly things like:
        "ls some | myfile.pl" (where myfile.pl starts with "while (<>)..."
        There must be a way to do this inside the perl script.  Please
        supply clue. note:  i tried "while (<system("/bin/ls -t1 some">)"
        \_ foreach $arg (@ARGV) {
               push(@dirs,$arg) if (-d $arg);
           }
           open(CMD,"/bin/ls -t1 @dirs|") || die "Error: $!\n";
           while(<CMD>) { &doStuff() ; }
           close(CMD);

           if you are interested in reading the contents of a directory
           consider using opendir, readdir and closedir instead of ls.
            \_yeah but i already know all the flags to order ls the way
                i want.  Anyway, i'm such a rookie i didn't even know
                you could turn a command into a filehandle, this is
                very cool, thanks.
           \_ what does the | in the open signify? --newbie
              \_ | -> pipe. open(CMD,"cmd|") does a popen() in read
                 mode, while open(CMD,"|cmd") does a popen() in write
                 mode. Note: you can't do open(CMD,"|cmd|"). If you
                 need this take a look at open2(). If you use open2()
                 your *must* save its return code which is the pid
                 of the process that open2() runs on your behalf.
                 You *must* call waitpid(pid,0) on this pid otherwise
                 you will end up with defunct processes.