(August 2016)
Here’s a good header for our Perl files:
#!/usr/bin/env perl
use v5.10;
use strict;
use warnings;
use autodie;
say scalar @ARGV; # The number of arguments.
say $ARGV[0]; # The first argument
say $0; # The name of this script.
my $filename = '/tmp/stuff.txt';
open my $fh, '<', $filename;
while (my $line = <$fh>) {
chomp $line;
say $line;
}
my $filename = '/tmp/stuff.txt';
open my $fh, '<', $filename;
my $contents = do { local $/; <$fh> };
See an explanation at http://www.perlmonks.org/?node_id=287647. In short, the built-in $/ tells Perl where to stop reading; it’s set to newline by default. We undefine $/ in this block scope in order to continue reading until the end of file.
say "Found the plain file $file_path" if -f $file_path
There are other file tests besides -f
. See:
$ perldoc -f '-X'
my $filename = '/tmp/stuff.txt';
open my $fh, '>', $filename;
print $fh "Here's a line of text\n";
To append rather than overwrite, open the filehandle with a double-carrot:
open my $fh, '>>', $filename;
use File::Find;
my @files;
my $root = "/home/paulgorman/tmp";
find(
sub { push @files, $File::Find::name unless -d; },
$root
);
for my $file (@files) {
say $file;
}
my @files = glob ('/home/paulgorman/tmp/*.class');
for my $file (@files) {
say $file;
}
print STDERR "We dun messed up.\n";
Avoid the issue by using system():
use IPC::System::Simple qw(system);
system('command', 'arg1', 'arg2');
But if we must, look at String::ShellQuote
.
$ cpan HTTP::Server::Brick
$ perl -MHTTP::Server::Brick -e
‘$s=HTTP::Server::Brick->new(port=>8000); $s->mount(“/”=>{path=>“.”}); $s->start’