Commits

Shlomi Fish committed 0dff951

Convert more.

Comments (0)

Files changed (2)

lib/PerlBegin/TopicsExamples.pm

         my $esc_id = CGI::escapeHTML($id);
         my $esc_label = CGI::escapeHTML($label);
 
-        my $code = <<"EOF";
+        my $post_code;
+
+        if ($ex_spec->{no_syntax})
+        {
+            $post_code = "<pre>\n" . CGI::escapeHTML($pre_code) . "</pre>\n";
+        }
+        else
+        {
+            my $code = <<"EOF";
 #!/usr/bin/perl
 
 use strict;
 $pre_code
 EOF
 
-        my $tvc = Text::VimColor->new(
-            string => \$code,
-            filetype => 'perl',
-        );
+            my $tvc = Text::VimColor->new(
+                string => \$code,
+                filetype => 'perl',
+            );
+
+            $post_code =
+                qq|<pre class="perl">\n|
+                . ($tvc->html() =~ s{(class=")syn}{$1}gr)
+                . qq|\n</pre>\n|
+                ;
+        }
 
         push @lis, qq[<li><a href="#$esc_id">$esc_label</a></li>\n];
-        push @codes, qq|<div id="$esc_id"><pre class="perl">\n|
-            . ($tvc->html() =~ s{(class=")syn}{$1}gr)
-            . qq|\n</pre>\n</div>|;
+        push @codes, qq[<div id="$esc_id">$post_code</div>];
     }
 
     return

src/topics/files-and-directories/index.html.wml

 <:{
     print PerlBegin::TopicsExamples->new(
         {
+            title => "Processing the Lines of a File",
+            id_base => "processing_lines_of_a_file",
+            examples =>
+            [
+                {
+                    id => "file_slurp",
+                    label => "File::Slurp",
+                    code => <<'EOF',
+use File::Slurp;
+
+my @filenames = @ARGV;
+
+foreach my $fn (@filenames)
+{
+    edit_file_lines sub { s/\bFrom\b/To/g }, $fn;
+}
+EOF
+
+                },
+                {
+                    id => "core",
+                    label => "Core Perl",
+                    code => <<'EOF',
+use autodie;
+use File::Temp ( qw(tempfile) );
+
+my @filenames = @ARGV;
+
+foreach my $fn (@filenames)
+{
+    open my $in, '&lt;', $fn;
+    my ($tempout, $temp_fn) = tempfile();
+
+    while (my $line = &lt;$in&gt;)
+    {
+        chomp($line);
+        \# Perform the operation here.
+        $line =~ s/\bFrom\b/To/g;
+
+        print {$tempout} "$new_line\n";
+    }
+
+    close($in);
+    close($tempout);
+
+    rename($temp_fn, $fn);
+}
+EOF
+                },
+                {
+                    id => "cmd_line",
+                    label => "Command Line",
+                    no_syntax => 1,
+                    code => <<'EOF',
+$ perl -i.bak -lp -e 's/\bFrom\b/To/g' *.txt
+EOF
+                },
+            ],
+        }
+    )->html_with_title();
+}:>
+
+<:{
+    print PerlBegin::TopicsExamples->new(
+        {
             title => "Overwriting a file with text",
             id_base => "overwrite_a_file",
             examples =>