CodeGen-Cpppp

 view release on metacpan or  search on metacpan

bin/cpppp  view on Meta::CPAN


=item --call 'METHOD(...)'

Shortcut for C<--eval> to call a method of C<$tpl>.

=item -o

=item --out [SECTION=]FILENAME[@MARKER]

Write output to FILENAME instead of C<stdout> (unless FILENAME is "-" then do
still write C<stdout> )  If FILENAME already exists, a backup will be created,
unless you specified a @MARKER.

If a @MARKER is specified, the content will be written within the existing
content of the file (without a backup) between lines that match
C<< BEGIN marker >> and C<< END marker >>.  If those lines are not found, the
operations aborts.

An equal sign indicates that only specific sections should be sent to this file.
The SECTION is either the name of one section, or a comma-delimited list of
section names, or a range C<A..B> of section names.  All content diverted to
this FILENAME will be omitted from further output.

=item --list-sections

lib/CodeGen/Cpppp.pm  view on Meta::CPAN

      {$1$new_content$2}sm
      or croak "Can't find $patch_markers in $fname";
   $fh->seek(0,0) or die "seek: $!";
   $fh->print($content) or die "write: $!";
   $fh->truncate($fh->tell) or die "truncate: $!";
   $fh->close or die "close: $!";
   $self;
}


sub backup_and_overwrite_file($self, $fname, $new_content) {
   $new_content .= "\n" unless $new_content =~ /\n\Z/;
   utf8::encode($new_content);
   if (-e $fname) {
      my $n= 0;
      ++$n while -e "$fname.$n";
      require File::Copy;
      File::Copy::copy($fname, "$fname.$n") or die "copy($fname, $fname.$n): $!";
   }
   open my $fh, '>', $fname or die "open($fname): $!";
   $fh->print($new_content) or die "write: $!";

lib/CodeGen/Cpppp.pm  view on Meta::CPAN

   }
   $content;
}


sub write_sections_to_file($self, $sections, $fname, $patch_markers=undef) {
   my $content= $self->get_filtered_output($sections);
   if (defined $patch_markers) {
      $self->patch_file($fname, $patch_markers, $content);
   } else {
      $self->backup_and_overwrite_file($fname, $content);
   }
   $self
}

sub _slurp_file($self, $fname) {
   open my $fh, '<', $fname or die "open($fname): $!";
   my $content= do { local $/= undef; <$fh> };
   $fh->close or die "close: $!";
   $content;
}

lib/CodeGen/Cpppp.pm  view on Meta::CPAN

between those two lines with C<$new_content>, and writes it back to the same
file handle.

Example:

  my $tpl= $cpppp->require_template("example.cp");
  my $out= $tpl->new->output;
  $cpppp->patch_file("project.h", "example.cp", $out->get('public'));
  $cpppp->patch_file("internal.h", "example.cp", $out->get('protected'));

=head2 backup_and_overwrite_file

  $cpppp->backup_and_overwrite_file($filename, $new_content);

Create a backup of $filename if it already exists, and then write a new file
containing C<$new_content>.  The backup is created by appending a ".N" to the
filename, choosing the first available "N" counting upward from 0.

=head2 get_filtered_output

  my $text= $cpppp->get_filtered_output(@sections);

Like C<< $cpppp->output->get >>, but also apply filters to the output, like
L</convert_linecomment_to_c89>.

=head2 write_sections_to_file

  $cpppp->write_sections_to_file($section_spec, $filename);
  $cpppp->write_sections_to_file($section_spec, $filename, $patch_markers);

This is a simple wrapper around L<CodeGen::Cpppp::Output/get> and either
L</backup_and_overwrite_file> or L</patch_file>, depending on whether you
supply C<$patch_markers>.

=head1 AUTHOR

Michael Conrad <mike@nrdvana.net>

=head1 VERSION

version 0.005



( run in 1.458 second using v1.01-cache-2.11-cpan-49f99fa48dc )