in reply to Simplifying code (Not obfuscation)
#! /usr/bin/perl -w chomp( my $var = `pwd` ); print "You are here -> $var <- in case you were lost\n";
That is, you can chomp the result of an assignment, because chomp knows how to operate on lvalues.
I've thrown in the my here to show how you can declare a variable, assign to it, and then chomp it, all in one go. Of course, if your variable is declared earlier (above) in the code then of course you don't have to do that.
A word of warning, beware of what happens when your system command returns more than one line. For instance, see what happens when you replace pwd with ps. In this case, you can also say:
chomp( my @proc = `ps` );On the other hand, I would spend the time to research what you really want to do. Nobody actually likes calling programs and scraping their output. So much so that you will often find that someone has written a module that gets at the underlying kernel tables or whatever and has published it on CPAN.
For instance, just the other day I had to do some wholesale disk quota management, and after staring at a few man pages for a while and wondering how I could automate it, a quick search on CPAN revealed the module Quota which did exactly what I needed (and more besides).
print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Simplifying code (Not obfuscation)
by 2mths (Beadle) on Mar 04, 2003 at 12:44 UTC | |
by thor (Priest) on Mar 04, 2003 at 13:42 UTC | |
by zakb (Pilgrim) on Mar 04, 2003 at 13:44 UTC |