in reply to Re: Using UNIX commands inside Perl scripts, with Perl variables
in thread Using UNIX commands inside Perl scripts, with Perl variables

I have no UNIX prompt at my hand, so I cannot compile this ... but as far as I remember, it goes like this:

I always use the pipe with the open command like this

open(IN,"ls -l |");
my @IN = <IN>;
close IN
Worx! The @IN array contains output from the system commando "ls -l" giving the file listing.
Erik Østergaard
  • Comment on Re: Re: Using UNIX commands inside Perl scripts, with Perl variables

Replies are listed 'Best First'.
Re: Re: Re: Using UNIX commands inside Perl scripts, with Perl variables
by stephen (Priest) on Jun 08, 2001 at 02:18 UTC
    I think Mr. Østergaard meant this:
    # Note -- bad way to do this open(IN, "ls -l |"); my @IN = <IN>; close(IN);
    For a better way of getting directory contents, see perlfunc:opendir and perlfunc:readdir.

    Which is a bit off-topic...

    stephen

      Nah, I meant this:
      # $command can be any system command, that would produce an acceptable output
      my $string = "my email is";
      my $dir = "/home/erik/*";
      my $command = "grep -ri '".$string."' ".$dir;
      open(IN, $command." |");
      my @IN = <IN>;
      close(IN);

      Better, Stephen? =) I agree, that opening dirs isn't the best purpose for this method.

      Erik