Re: Need help with Piping
by lidden (Curate) on Aug 13, 2008 at 05:56 UTC
|
#!/usr/bin/perl -w
use strict;
my $var = '';
while(<>){
$var .= $_;
}
# Do stuff with $var
| [reply] [d/l] |
|
|
my $var;
SLURP: { local $/;
$var= <>;
}
local-izing $/ in a block undefines it and so no $var gets the whole filecontent sithout looping and reassembling it line by line.
s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
+.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
| [reply] [d/l] [select] |
|
|
| [reply] [d/l] [select] |
|
|
my $var = do { local $/ ; <> };
| [reply] [d/l] |
Re: Need help with Piping
by Bloodnok (Vicar) on Aug 13, 2008 at 10:39 UTC
|
Using a one-liner and backticks, perl -e '{ .... }' `ls -l`, @ARGV will be loaded with the output from the ls command.
However, if you wanted to process each line in turn, you could use a one-liner ... ls -l | perl -ne '{ .... }' to read each line of the ls command, in turn, into $_ or alternatively ...
ls -l | perl -ane '{ .... }' will read each line in turn and split it (the line) on whitespace, into @F.
HTH ,
A user level that continues to overstate my experience :-))
| [reply] [d/l] [select] |
Re: Need help with Piping
by eosbuddy (Scribe) on Aug 13, 2008 at 05:43 UTC
|
use strict;
use warnings;
print "$_\n" while (<>);
Update (multiple): apologies, didn't see the $var part. I assume, you are going to use it as a list:
my @var;
push(@var, $_) while(<>);
| [reply] [d/l] [select] |
|
|
I forgot to mention that you will need to chomp depending on the situation (e.g. ls adds a "\n" automatically when piped while other commands may not behave in a similar manner).
| [reply] [d/l] [select] |
Re: Need help with Piping
by zentara (Cardinal) on Aug 13, 2008 at 16:14 UTC
|
You have some solutions for getting STDIN, but do you really need separate scripts? Why not let a.pl run 'ls' or your command? Trying to force things thru STDIN is often frought with difficulty and should be avoided if possible, unless you need to build a chain of commands for some reason. Just use a piped open, or read perldoc perlipc for the many other ways to do it.
#!/usr/bin/perl
open (LS,"ls |" ) or die $!;
my @files = ();
while(<LS>){
chomp;
push @files, $_;
}
print "@files\n";
| [reply] [d/l] |
Re: Need help with Piping
by blazar (Canon) on Aug 14, 2008 at 17:10 UTC
|
I personally believe that quite a full overview of all the possibilities has already been offered to you. But there's a last one missing, that I'm reporting here FYI and for completeness: the implicit open associated with the magic ARGV filehandle is the 2-args one. (Which incidentally I regard as problematic in many senses...) Thus you can pass commands to your script on the command line:
spock:~ [18:59:27]$ perl -0777e 'my $v = <>; print $v' 'ls|'
Desktop
Mail
Mailbox
bin
dead.letter
...
| [reply] [d/l] [select] |