German-speaking magazine for Perl beginners (see
L<http://perl-zeitung.at.tf> ).
-Several unix/linux releated magazines frequently includes articles on Perl.
+Several unix/linux related magazines frequently includes articles on Perl.
=head2 Which Perl blogs should I read?
More complex cases will require to write a parser, probably
using a parsing module from CPAN, like
L<Regexp::Grammars>, L<Parse::RecDescent>, L<Parse::Yapp>,
-L<Text::Balanced>, or L<Marpa::XS>.
+L<Text::Balanced>, or L<Marpa::R2>.
=head2 How do I reverse a string?
print $out $content;
-Modules such as L<File::Slurp> and L<Tie::File> can help with that
+Modules such as L<Path::Tiny> and L<Tie::File> can help with that
too. If you can, however, avoid reading the entire file at once. Perl
won't give that memory back to the operating system until the process
finishes.
while( sysread $fh, $buffer, 4096 ) {
$lines += ( $buffer =~ tr/\n// );
}
- close FILE;
+ close $fh;
However, that doesn't work if the line ending isn't a newline. You
might change that C<tr///> to a C<s///> so you can count the number of
while( sysread $fh, $buffer, 4096 ) {
$lines += ( $buffer =~ s|$/||g; );
}
- close FILE;
+ close $fh;
If you don't mind shelling out, the C<wc> command is usually the
fastest, even with the extra interprocess overhead. Ensure that you
accessing an element of the array actually accesses the corresponding
line in the file.
-If you want to load the entire file, you can use the L<File::Slurp>
+If you want to load the entire file, you can use the L<Path::Tiny>
module to do it in one simple and efficient step:
- use File::Slurp;
+ use Path::Tiny;
- my $all_of_it = read_file($filename); # entire file in scalar
- my @all_lines = read_file($filename); # one line per element
+ my $all_of_it = path($filename)->slurp; # entire file in scalar
+ my @all_lines = path($filename)->lines; # one line per element
Or you can read the entire file contents into a scalar like this:
<brackets in <nested brackets> >
<another group <nested once <nested twice> > >
-With a little extra work, you can get the all of the groups in angle
+With a little extra work, you can get all of the groups in angle
brackets even if they are in other angle brackets too. Each time you
get a balanced match, remove its outer delimiter (that's the one you
just matched so don't match it again) and add it to a queue of strings
}
The output shows all of the groups. The outermost matches show up
-first and the nested matches so up later:
+first and the nested matches show up later:
Found:
<brackets in <nested brackets> >