muizelaar has asked for the wisdom of the Perl Monks concerning the following question:

I have written this small script to list all our Solaris packages on our servers. I require that it only lists our own application software listed in the Array. However if there are no packages listed I would like the script to be able to tell me there are no packages installed rather than show a blank screen. I have played around with various methods but cant seem to get anything to work, Any help would be appreciated.
#!/usr/bin/perl -w use Cwd; my $dir = getcwd; my $newdir = "/opt/DSPKG"; my $pkg = "DSDSLVP*"; my $PKG = "DSAPEVP*"; #Changes directory chdir($newdir) || die "Can't Chdir to $newdir: $!"; system("pkginfo $pkg $PKG > /tmp/pkglist 2> /tmp/vperror"); open(MYFILE, "/tmp/pkglist") || die "can't open file: $!"; @stuff=<MYFILE>; print "\n"; foreach my $file (@stuff) {eval { print "$file"; }} print "\n\n"; print "Press ENTER to Continue?"; my $answer = <STDIN>; chomp $answer;

Replies are listed 'Best First'.
Re: Array output
by moritz (Cardinal) on Sep 17, 2007 at 11:10 UTC
      Hi Thanks for that its working now. I tried a simmillar approch but I think I didnt use the Join function as part of my script. Still learning :)
Re: Array output
by cdarke (Prior) on Sep 17, 2007 at 11:50 UTC
    I notice that you do not delete the temporary file at end, but do you need it? You can read the output from a program direct into an array (or scalar) with qx (or `back-ticks`):
    @stuff = qx(pkginfo $pkg $PKG 2> /tmp/vperror);
    This replaces the system and open commands, and the read. stderr is not affected, so you might want to unlink the file.
Re: Array output
by shmem (Chancellor) on Sep 17, 2007 at 14:08 UTC
    Something that immediately caught my eye:
    foreach my $file (@stuff) {eval { print "$file"; }}

    Why the eval ?
    Don't double-quote single variables, it's a bad habit unless needed.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}