Devel::REPL::Plugin::DumpHistory - Plugin for Devel::REPL to save or print


Devel-REPL documentation Contained in the Devel-REPL distribution.

Index


Code Index:

NAME

Top

Devel::REPL::Plugin::DumpHistory - Plugin for Devel::REPL to save or print the history.

SYNOPSIS

Top

    #!/usr/bin/perl 

    use lib './lib';
    use Devel::REPL;

    my $repl = Devel::REPL->new;
    $repl->load_plugin('LexEnv');
    $repl->load_plugin('History');
    $repl->load_plugin('DumpHistory');
    $repl->run;

DESCRIPTION

Top

Plugin that adds the :dump and :dump file_name commands to the repl which will print the history to STDOUT or append the history to the file given.

SEE ALSO

Top

Devel::REPL

AUTHOR

Top

mgrimes, <mgrimes at cpan dot org<gt>

COPYRIGHT AND LICENSE

Top


Devel-REPL documentation Contained in the Devel-REPL distribution.

package Devel::REPL::Plugin::DumpHistory;

use Devel::REPL::Plugin;
use namespace::clean -except => [ 'meta' ];

## Seems to be a sequence issue with requires
# requires qw{ history }; 

around 'read' => sub {
  my $orig = shift;
  my ($self, @args) = @_;

  my $line = $self->$orig(@args);
  if (defined $line) {
    if ($line =~ m/^:dump ?(.*)$/) {
      my $file = $1;
      $self->print_history($file);
      return '';
    }
  }
  return $line;
};

sub print_history {
    my ( $self, $file ) = @_;

    if ($file) {
        open( my $fd, ">>", $file )
            or do { warn "Couldn't open '$file': $!\n"; return; };
        print $fd "$_\n" for ( @{ $self->history } );
        $self->print( sprintf "Dumped %d history lines to '$file'\n",
            scalar @{ $self->history } );
        close $fd;
    } else {
        $self->print("$_\n") for ( @{ $self->history } );
    }
    return 1;
}

1;

__END__