Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Running operating system commands in Perl

Perl is excellent "glueware" - a language which can be well used to connect together a whole lot of elements. And one of those elements may be operating system commands run from within the Perl program.

Here are (three!) ways that you can do it - in each case leaving the result of running the commands in a scalar variable.

# CAUTION - OS Dependent!
 
$imc = `uname -m`;
 
$imd = qx!uname -r!;
 
$imcalled = <<`DONE`
uname -s
date
uptime
DONE
;
 
# CAUTION - OS Dependent!
 
print ("***** Here document:\n$imcalled\n");
print ("***** Backquote:\n$imc\n");
print ("***** qx:\n$imd");


So that's
* A text string of commands in backquotes
* A text string of commands in a 'qx' string
* A text block (here document)

And here's how it runs:

BirthdayBoy:csr1 grahamellis$ perl bq
***** Here document:
Darwin
Tue 8 Jul 2008 18:41:38 BST
18:41 up 31 mins, 2 users, load averages: 0.01 0.05 0.06
 
***** Backquote:
Power Macintosh
 
***** qx:
9.0.0
BirthdayBoy:csr1 grahamellis$


You can also run operating system commands via a piped file handle, where you'll read back the response line by line:

open (FH,"ls -l *.*|");
while ($line = <FH>) {
  print ++$n," ",$line;
  }


Which gives us ...

BirthdayBoy:csr1 grahamellis$ perl lbl
1 -rw-r--r-- 1 grahamellis grahamellis 2856 7 Jul 16:35 dis.txt
2 -rw-r--r-- 1 grahamellis grahamellis 825 8 Jul 09:24 fred.txt
3 -rw-r--r-- 1 grahamellis grahamellis 3 7 Jul 10:04 pod2htmd.tmp
4 -rw-r--r-- 1 grahamellis grahamellis 3 7 Jul 10:04 pod2htmi.tmp
5 -rw-r--r-- 1 grahamellis grahamellis 502 8 Jul 14:28 tick.pm
BirthdayBoy:csr1 grahamellis$



(By the way - the name of my computer today - "BirthdayBoy" - has been allocated to me by the router at the hotel and does not imply anything about the day I was born!)
(written 2008-07-08, updated 2008-07-10)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P214 - Perl - System Dependencies
  [2825] Perl course - is it tailored to Linux, or Microsoft Windows? - (2010-06-25)
  [733] Perl for Systems Admin - suid scripts - (2006-05-25)

P258 - Perl - Solaris, OSX and Linux
  [2834] Teaching examples in Perl - third and final part - (2010-06-27)
  [2022] Pre and post increment - the ++ operator - (2009-02-03)
  [1633] Changing a screen saver from a web page (PHP, Perl, OSX) - (2008-05-06)


Back to
Training Course and Business Conference Technology
Previous and next
or
Horse's mouth home
Forward to
Perl ... adding to a list - end, middle, start
Some other Articles
The Story of Well House Manor
Environment variables in Perl / use Env
Finding operating system settings in Perl
Perl ... adding to a list - end, middle, start
Running operating system commands in Perl
Training Course and Business Conference Technology
FTP server on Fedora Linux
If you are learning Lua, here are some more examples
Dealing with The Press
Sorting in lua - specifying your own sort routine
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/1702_Run ... -Perl.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat