in reply to Re^3: RFC: newscript.pl , my very first script!
in thread RFC: newscript.pl , my very first script!

What I need to do:

- make use of environment variable to determine which is the default editor and make use of it, instead of assuming emacs. (not sure how to do that yet)

Commonly, the EDITOR or VISUAL environment variables specify which editor executable to use. If both are unset or empty, use some arbitary fallback. The debian people use a bunch of symlinks to specify a default editor in /usr/bin/editor.

The two environment variables may contain only the name, not the absolute path of the editor. So you also have to search $ENV{'PATH'}.

Different editors have different command line options. The only thing that you can rely on is that the editor will accept a single argument containing a filename to be edited. In other words: no extra command line options as long as you allow having different editors.

#!/usr/bin/perl use strict; use warnings; use List::Util qw( first ); sub exec_editor { my $filename=shift; my @path=split /:/,$ENV{'PATH'}; my $editor=first { length($_) && -f($_) && -x(_) } map { my $file=$_; /\// ? $file : map { "$_/$file" } @path } ( $ENV{'EDITOR'}//'', $ENV{'VISUAL'}//'', '/usr/bin/editor', # for Debian and friends qw( joe emacs nano vi ), ); defined($editor) or die "Can't find an editor"; exec($editor,$filename) or die "Can't execute $editor: $!"; } @ARGV==1 or die "Usage: $0 file-to-edit\n"; exec_editor($ARGV[0]);

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)