#! /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'

In reply to Re: Simplifying code (Not obfuscation) by grinder
in thread Simplifying code (Not obfuscation) by 2mths

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.