Perl beginner, working on my first program as practice. At first, I thought that it worked, but it was quickly revealed that it was only functional under ideal circumstances.

The aim of the code is to concatenate the output of lsatter and ls -l on one line, without repeating the filename field, which is found in both. Good output looks like this:
-------------e-- drwxr-xr-x 2 root root 4096 Apr 24 12:02 cron.hourly
In which the first field is from lsattr and the rest from ls.

However, in some cases there will be files that present lsattr with errors, such as links. In which case, the output of lsattr was skipped because perl is not reading STDERR. So the entire output is wrong from that point on. What I would like to do is capture the STDERR and use an if statement to print N/A where lsattr is not relevant. However, using 2>&1 to catch the STDERR does not maintain to order in which the errors originally appeared, also ruining the output.

#!/usr/bin/perl $num_args = $#ARGV + 1; my @list = `ls -lU`; chomp (my @chattr = `lsattr 2>&1 | sort`); my $value = 0; my @chats; my $arg = $ARGV[0]; my $argVal = 0; my $index = 0; foreach my $item (@list) { @chats = split(/ /, $chattr[$value]); if(index($chats[0], "lsattr:") == -1) { print "$item $chats[0] "; } else { print "$item error"; } if (index($chats[0], $arg) != -1) { $argVal++; } $value++; } if ($num_args != 0) { print "$arg: $argVal"; } print "\n\n";

If you are confused regarding ARGV, the code also allows the user to input a chattr attribute and get a number of times that attribute appears.

My question then, of course, is how should I proceed with my code to correct the issues? I would like to stay away from cpan modules, if possible.


In reply to STDERR, preserve order of by GillSans

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.