#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
my $file = 'file.txt';
open(my $fh, "<", $file)
or die "Can't open ".$file." error: $!";
while( defined( my $line = <$fh> ) ) {
chomp $line;
next if $line =~ /^\s*$/; # skip empty lines
next if (index($line, 'second') != -1);
say $line;
}
close $fh
or warn "Can't close ".$file." error: $!";
__DATA__
This is the first line in the file.
This is the second line in the file.
This is the third line in the file.
This is the forth line in the file.
__OUTPUT__
$ perl test.pl
This is the first line in the file.
This is the third line in the file.
This is the forth line in the file.
####
#!/usr/bin/perl
use strict;
use IO::All;
use warnings;
use Data::Dumper;
use feature 'say';
my $io = io 'file.txt';
# Miscellaneous:
my @lines = $io->chomp->slurp;
print Dumper \@lines;
# Tie::File support:
$io->[2] = 'This is the changed line in the file.'; # Change a line
say $io->[@$io / 2]; # Print middle line
print Dumper \@lines;
__DATA__
This is the first line in the file.
This is the second line in the file.
This is the third line in the file.
This is the forth line in the file.
__OUTPUT__
$ perl test.pl
$VAR1 = [
'This is the first line in the file.',
'This is the second line in the file.',
'This is the third line in the file.',
'This is the forth line in the file.'
];
This is the changed line in the file.
$VAR1 = [
'This is the first line in the file.',
'This is the second line in the file.',
'This is the changed line in the file.',
'This is the forth line in the file.'
];
####
#!/usr/bin/perl
use strict;
use warnings;
use Capture::Tiny 'capture';
my $original = 'file.txt';
my $copy = 'fileTest.txt';
my $cmd = qq{cp -v $original $copy};
# capture from external command
my ($stdout, $stderr, $exitCode) = capture {
system( $cmd );
};
print 'StdOut: ' . $stdout if $exitCode == 0;
print 'Error: ' . $stderr unless $exitCode == 0;
__END__
$ perl test.pl
StdOut: 'file.txt' -> 'fileTest.txt'