Quantcast

Maximum PC

It is currently Thu Oct 30, 2014 4:09 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: perl IO woe
PostPosted: Sat Apr 23, 2005 1:36 am 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Thu Jul 22, 2004 11:11 am
Posts: 5108
Location: RFC 1149 - Pidgeon Network
i'm trying to execute a command on the console from a perl prog and capture the output.
Code:
$cmd = "dir";   
open(outF, '>x.txt') || die;
open(STATUS,  "$cmd |") || die "can't fork: $!";
    while (<STATUS>) {
   print <STATUS>;   
       @cmdoutput = <STATUS>;
        print outF @cmdoutput;
    }
    @cmdoutput = <STATUS>;
    print outF @cmdoutput;
    close STATUS || die "bad command: $! $?"; 
close(outF);


I've used perl before on snmp, but that was more of a get stuff working as i go along project. I'm not sure what type <STATUS> is, and I want to get that to a file or unterstand how to use that variable. i've been mucking aaround for a while and it's 5:30 am, so forgive any blatant mistakes :)


Top
  Profile  
 
 Post subject:
PostPosted: Sat Apr 23, 2005 6:03 am 
In the lab!
In the lab!
User avatar

Joined: Sun Jun 06, 2004 10:47 am
Posts: 831
Location: Secret Laboratory
tee hee hee... man I love perl! So cryptic and confusing. :)

<STATUS> is a file handle

open(STATUS, "$cmd |")... - this opens whatever is in the quotes and gives it a handle of STATUS (normally you open a file but perl lets you open really anything that can be treated as a file)
while (<STATUS>) - that's basically saying while this "file" is open do something
@cmdoutput = <STATUS> - this says dump everything from the "file" into an array where each line of the "file" is a seperate row of the array.


Top
  Profile  
 
 Post subject:
PostPosted: Sat Apr 23, 2005 6:15 am 
In the lab!
In the lab!
User avatar

Joined: Sun Jun 06, 2004 10:47 am
Posts: 831
Location: Secret Laboratory
Oh yea and you have a bug in the code...
Putting that "print <STATUS>;" in there causes the contents of the "file" to be read in and printed to the screen. Which then means when you hit the next line "@cmdoutput = <STATUS>;" the contents of the "file" is now empty and the array gets filled with a null.

the code works fine otherwise and will dump the contents of a programs output into the file x.txt... just remove the print <status> and put a print @cmdouput after the @cmdoutput = <status> line.


Top
  Profile  
 
 Post subject:
PostPosted: Sat Apr 23, 2005 2:10 pm 
Bitchin' Fast 3D Z8000
Bitchin' Fast 3D Z8000
User avatar

Joined: Thu Jul 22, 2004 11:11 am
Posts: 5108
Location: RFC 1149 - Pidgeon Network
Thanks dexter
Beautiful almost done ver1 :)

making a remote console to my pc through aim. yeah, perl rocks :)


Top
  Profile  
 
 Post subject:
PostPosted: Sat Apr 23, 2005 2:30 pm 
In the lab!
In the lab!
User avatar

Joined: Sun Jun 06, 2004 10:47 am
Posts: 831
Location: Secret Laboratory
coolies... sounds like a fun little project!


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.