Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Simplifying code (Not obfuscation)

by grinder (Bishop)
on Mar 04, 2003 at 11:27 UTC ( #240293=note: print w/replies, xml ) Need Help??


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
    lvalues = more homework.
    Multiple lines returned... - The norm with NT cmdline tools it seems. I'm sure I can code to cope with that.
    Quota - Sound really interesting, must go and have a look at that!
    CPAN - Doesn't strike me as very Win32 or PPM friendly. As an Win32 dumbo I probably 'scrape' more often that I have to but it saves on dissapointment.
      As for me, I look to CPAN first. If there is something there, chances are that
      • The author put some time in to it (so why should I duplicate his effort, wasting both my time and his)
      • The author put some thought in to it (probably more that I would "hand rolling" a solution)
      • The code is peer reviewed, meaning that gotchas have a better chance to be caught.

      thor

      It's always worth checking PPM for the CPAN modules you want - usually there's a PPM version for the less obscure ones.

      Alternatively, some of the monks have their own PPM repositories which further extends the possibilities, see this thread for info.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://240293]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2022-12-03 23:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?