Click here to Skip to main content
13,457,971 members
Click here to Skip to main content
Add your own
alternative version


5 bookmarked
Posted 13 Jul 2012

How to pipe console apps

Rate this:
Please Sign up or sign in to vote.
Simulate user interaction in console apps with perl


This post describes how to interact with an interactive console application and similate user interaction.  In this case we are goint to interact with the app called mongo.exe wich is the console client for the NoSql database MongoDb.


Command line applications have been very useful because they allow us to perform actions easy and fast, the problem here is when you need to call a console application and such application require user interaction. Sometimes it becomes a headache for developers who want to call those apps to perform x or y actions, so here comes Perl to help us.

Using the code 

The following piece of code shows how to create a set of actions to execute when we call a console app.
#create a set of instructions
my @instructions = ();

push(@instructions, qq{use test;});
push(@instructions, qq{});
push(@instructions, qq{{name:"Code complete", author:"Steve Mcconnell"});});
push(@instructions, qq{{name:"Perl: The Complete Reference", author:"Martin Brown"});});
push(@instructions, qq{;});
push(@instructions, qq{exit});

#pipe the app in a handler
open MONGO, "| mongo.exe" || die "can't pipe mongo.exe";
#execute set of instructions
print MONGO join("\n",@instructions);

 To execute the previous code you just need to type perl

Points of Interest 

Simulate user interaction in console apps may be tricky with other languages, in Perl is easy.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Carlos Luis Rojas Aragonés
Software Developer
Costa Rica Costa Rica
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03-2016 | 2.8.180323.1 | Last Updated 13 Jul 2012
Article Copyright 2012 by Carlos Luis Rojas Aragonés
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid