App-Framework
view release on metacpan or search on metacpan
lib/App/Framework/Extension/Filter.pm view on Meta::CPAN
=item B<init_class([%args])>
Initialises the object class variables.
=cut
sub init_class
{
my $class = shift ;
my (%args) = @_ ;
# Add extra fields
$class->add_fields(\%FIELDS, \%args) ;
# init class
$class->SUPER::init_class(%args) ;
}
#============================================================================================
=back
=head2 OBJECT METHODS
=over 4
=cut
#============================================================================================
#----------------------------------------------------------------------------
=item B<filter_run($app, $opts_href, $args_href)>
Filter the specified file(s) one at a time.
=cut
sub filter_run
{
my $this = shift ;
my ($app, $opts_href, $args_href) = @_ ;
## save for later
$this->_filter_opts($opts_href) ;
$this->_dbg_prt(["Args=", $args_href, "Opts=", $opts_href]) ;
# Get command line arguments
my @args = @{ $args_href->{'file'} || [] } ;
my @args_fh = @{ $args_href->{'file_fh'} || [] } ;
## check for in-place editing on STDIN
if ($opts_href->{inplace})
{
if ( (scalar(@args) == 1) && ($args_fh[0] == \*STDIN) )
{
$this->throw_fatal("Cannot do in-place editing of standard input") ;
}
}
$this->_dispatch_entry_features(@_) ;
#$this->debug(2) ;
$this->_dbg_prt(["#!# Hello, Ive started filter_run()...\n"]) ;
## Update from options
$this->feature('Options')->obj_vars($this, [keys %FIELDS]) ;
## Set up filter state
my $state_href = $this->_filter_state ;
$state_href->{num_files} = scalar(@args) ;
$state_href->{file_number} = 1 ;
$state_href->{file_list} = \@args ;
$state_href->{vars} = {} ;
## do each file
for (my $fnum=0; $fnum < $state_href->{num_files}; ++$fnum)
{
$state_href->{file_number} = $fnum+1 ;
$state_href->{outfile} = '' ;
$state_href->{line_num} = 1 ;
$state_href->{output_lines} = [] ;
$state_href->{file} = $args[$fnum] ;
$this->_dispatch_label_entry_features('file', $app, $opts_href, $state_href) ;
$this->_start_output($state_href, $opts_href) ;
## call application start
$this->call_extend_fn('app_start_fn', $state_href) ;
## Process file
my $fh = $args_fh[$fnum] ;
my $line ;
while(defined($line = <$fh>))
{
chomp $line ;
## see if line needs processing
if ($opts_href->{trim_space})
{
$line =~ s/^\s+// ;
$line =~ s/\s+$// ;
}
if ($opts_href->{trim_comment} && $opts_href->{comment})
{
$line =~ s/$opts_href->{comment}.*$// ;
}
$state_href->{line} = $line ;
$state_href->{output} = undef ;
$this->_dispatch_label_entry_features('line', $app, $opts_href, $state_href) ;
## see if we skip this line
lib/App/Framework/Extension/Filter.pm view on Meta::CPAN
$this->_dbg_prt(["_open_output\n"]) ;
my $outfile ;
if ($this->inplace)
{
## Handle in-place editing
$outfile = $state_href->{file} ;
$this->_dbg_prt([" + inplace file=$outfile\n"]) ;
}
elsif ($this->outfile)
{
## See if writing to dir
my $dir = $this->outdir ;
if ($dir)
{
## create path
mkpath([$dir], $this->debug, 0755) ;
}
$dir ||= '.' ;
my %opts = $this->options() ;
my %app_vars = $this->vars() ;
my %filter ;
$filter{'filter_fmt'} = $this->outfile ;
$filter{'filter_file'} = $state_href->{file} ;
$filter{'filter_filenum'} = $state_href->{file_number} ;
my ($base, $path, $ext) = fileparse($state_href->{file}, '\..*') ;
$filter{'filter_name'} = $base ;
$filter{'filter_base'} = $base ;
$filter{'filter_path'} = $path ;
$filter{'filter_ext'} = $ext ;
$this->expand_keys(\%filter, [\%opts, \%app_vars, \%ENV]) ;
$outfile = $filter{'filter_fmt'} ;
$this->_dbg_prt([" + eval=$@\n"]) ;
$this->_dbg_prt([" + outfile=$outfile: dir=$dir fmt=$filter{'filter_fmt'} file=$filter{'filter_file'} num=$filter{'filter_filenum'} base=$base path=$path\n"]) ;
$outfile = File::Spec->catfile($dir, $outfile) ;
}
## Output file specified?
if ($outfile)
{
$outfile = File::Spec->rel2abs($outfile) ;
my $infile = File::Spec->rel2abs($state_href->{file}) ;
if ($outfile eq $infile)
{
# In place editing - make sure flag is set
$this->inplace(1) ;
$this->_dbg_prt([" + inplace $outfile\n"]) ;
}
# else
# {
## Open output
open my $outfh, ">$outfile" or $this->throw_fatal("Unable to write \"$outfile\" : $!") ;
$this->out_fh($outfh) ;
$this->_dbg_prt([" + opened $outfile fh=$outfh\n"]) ;
$state_href->{outfile} = $outfile ;
# }
}
else
{
## STDOUT
$this->out_fh(\*STDOUT) ;
}
}
#----------------------------------------------------------------------------
=item B<_close_output($state_href, $opts_href)>
Close the file if open
=cut
sub _close_output
{
my $this = shift ;
my ($state_href, $opts_href) = @_ ;
my $fh = $this->out_fh ;
$this->set('out_fh' => undef) ;
if ($this->outfile)
{
close $fh ;
}
else
{
## STDOUT - so ignore
}
}
#----------------------------------------------------------------------------
=item B<_wr_output($state_href, $opts_href, $line)>
End of output file
=cut
sub _wr_output
{
my $this = shift ;
my ($state_href, $opts_href, $line) = @_ ;
my $fh = $this->out_fh ;
$this->_dbg_prt(["_wr_output($line) fh=$fh\n"]) ;
if ($fh)
( run in 1.175 second using v1.01-cache-2.11-cpan-39bf76dae61 )