Proc-InvokeEditor
view release on metacpan or search on metacpan
lib/Proc/InvokeEditor.pm view on Meta::CPAN
my @editors = @{$self->{'editors'}};
unshift @editors, @$edit;
$self->{'editors'} = \@editors;
}
sub editors_env {
my $self = shift;
my $edit = shift;
assert(ref($edit) eq 'ARRAY');
my @editors;
if (@$edit) {
foreach my $e (@$edit) {
if (exists $ENV{$e} and defined $ENV{$e}) {
push @editors, $ENV{$e};
}
}
my @editors_list = @{$self->{'editors'}};
unshift @editors_list, @editors;
$self->{'editors'} = \@editors_list;
}
return $self->{'editors'};
}
sub cleanup {
my $self = shift;
my $cleanup = shift;
$self->{'cleanup'} = $cleanup if defined $cleanup;
return $self->{'cleanup'};
}
sub keep_file {
my $self = shift;
my $keep_file = shift;
$self->{'keep_file'} = $keep_file if defined $keep_file;
return $self->{'keep_file'};
}
sub edit {
my $self = shift;
my $arg = shift;
my $suff = shift;
# if the argument supplied is a reference to an array of lines,
# join it together based on the input record separator
if (ref($arg) eq 'ARRAY') {
$arg = join $/, @$arg;
}
my $result;
if (ref($self)) {
($result, $self->{'filename'}) = _edit(
$arg,
$self->{'editors'},
$self->{'cleanup'},
$self->{'keep_file'},
$self->{'filename'},
$suff,
);
} else {
($result) = _edit($arg, \@DEFAULT_EDITORS, 1, 0, undef, $suff);
}
if (wantarray) {
my @result = split m|$/|, $result;
return @result;
} else {
return $result;
}
}
sub first_usable {
my $self = shift;
my $er = shift;
my @editors;
if (defined $er) {
@editors = @$er;
} else {
if (ref $self) {
@editors = @{$self->{'editors'}};
} else {
@editors = @DEFAULT_EDITORS;
}
}
my $chosen_editor;
my @path = File::Spec->path;
EDITORS: foreach my $editor (@editors) {
next unless defined $editor;
my @editor_bits;
if( $^O =~ /mswin/i ) {
require Text::ParseWords;
$editor =~ s!\\!\\\\!g; # quote path for shellwords
@editor_bits = Text::ParseWords::shellwords($editor);
} else {
@editor_bits = split /\s+/, $editor;
};
next unless defined $editor_bits[0];
if (File::Spec->file_name_is_absolute($editor_bits[0])
and -x $editor_bits[0]) {
$chosen_editor = \@editor_bits;
last;
} else {
foreach my $dir (@path) {
my $file = File::Spec->catfile($dir, $editor_bits[0]);
if (-x $file) {
$editor_bits[0] = $file;
$chosen_editor = \@editor_bits;
last EDITORS;
}
}
}
}
die "Couldn't find an editor: $!" unless defined $chosen_editor;
return $chosen_editor;
}
sub _edit {
my $string = shift;
my $er = shift;
my $unlink = shift;
my $keep_file = shift;
my $filename = shift;
my $suff = shift;
( run in 0.650 second using v1.01-cache-2.11-cpan-71847e10f99 )