Reputation: 186
I want to read html files entered from STDIN perform some function and then write another html file to STDOUT. My problem is I have to give file in the stated manner. I have tried many things but nothing is working good. Nothing is getting printed
my command line prompt
perl universe.pl<sun.html>galaxy.html
my code -
open(my $in, "<&STDIN") or die "Can't dup STDIN: $!";
open(my $out, ">&STDOUT") or die "Can't dup STDOUT: $!";
my @lines = <$in>;
foreach(@lines) {
push(@newlines,$_);
say "Lines pushing:", $_;
}
Upvotes: 0
Views: 3368
Reputation: 1
Below is my own effort:
#!/bin/perl
while (<>)
{
print $_;
}
I like to start from the simplest possible example before I undertake more complex tasks.
Being barely an intermediate-level perl programmer, I am not sure how critical these statements are and what they are for:
use strict; use warnings;
... so I temporarily omitted them from the start of my script and it still worked.
Upvotes: -2
Reputation: 19
I had this problem with a script which was run from inside another Perl script.
The solution was to set $| in the second script (the one which executes the script which reads from STDIN).
Upvotes: -1
Reputation: 754900
You don't need to open STDIN or STDOUT; they're always ready opened.
You don't need to slurp the whole file into memory as you do with:
my @lines = <$in>;
You never use $out
which should be indicative of a problem.
while (<>)
{
print mapping_func($_);
}
where mapping_func()
is your function that does the relevant transform on its input string and returns the mapped result:
sub mapping_func
{
my($string) = @_;
$string =~ s/html/HTML/gi;
return $string;
}
Upvotes: 2
Reputation: 185700
Using the magic diamond operator <>
, you will able to do what you asked. But please to provide some more search efforts next time.
use strict; use warnings;
while (my $line = <>) {
# do something with $line
}
Last but not least; if you have to parse HTML the good way, have a look to HTML::TreeBuilder::XPath or just HTML::TreeBuilder
Upvotes: 1