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". ;-)

In reply to Re^4: RFC: newscript.pl , my very first script! by afoken
in thread RFC: newscript.pl , my very first script! by Darfoune

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.