Difference between revisions of "Open perl"

From Teknologisk videncenter
Jump to: navigation, search
m (New page: == Piping from Perl to system commands == === Getting STDOUT from commands === <source lang=perl> #!/usr/bin/env perl print "Jeg er et perl program\n"; open FH,"ls -l|"; foreach my $i (...)
 
m (Getting STDOUT from commands)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
== Piping from Perl to system commands ==
 
== Piping from Perl to system commands ==
=== Getting STDOUT from commands ===
+
== Getting STDOUT from commands ==
 +
=== Metode 1 ===
 +
Læser linierne ind en efter en som de behandles
 
<source lang=perl>
 
<source lang=perl>
 
#!/usr/bin/env perl
 
#!/usr/bin/env perl
Line 10: Line 12:
 
foreach my $i (<FH>) {
 
foreach my $i (<FH>) {
 
  print "Har modtaget linien: $i";
 
  print "Har modtaget linien: $i";
 +
}
 +
</source>
 +
=== Metode 2 ===
 +
Henter alle linier før de behandles
 +
<source lang=perl>
 +
#!/usr/bin/perl
 +
 +
my @files = `ls -l`;
 +
 +
for my $i (@files) {
 +
print "Der er $i\n";
 
}
 
}
 
</source>
 
</source>
 
[[Category:Perl]]
 
[[Category:Perl]]

Latest revision as of 04:37, 21 November 2009

Piping from Perl to system commands

Getting STDOUT from commands

Metode 1

Læser linierne ind en efter en som de behandles

#!/usr/bin/env perl

print "Jeg er et perl program\n";

open FH,"ls -l|";

foreach my $i (<FH>) {
 print "Har modtaget linien: $i";
}

Metode 2

Henter alle linier før de behandles

#!/usr/bin/perl

my @files = `ls -l`;

for my $i (@files) {
 print "Der er $i\n";
}