Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

How to open an editor from your Perl script?

by dr.jekyllandme (Sexton)
on Jul 24, 2012 at 00:27 UTC ( #983265=perlquestion: print w/ replies, xml ) Need Help??
dr.jekyllandme has asked for the wisdom of the Perl Monks concerning the following question:

So I want to create a Perl script that will open a file in an editor. This allows the user to make changes to the file and save those changes. Once the user exits the editor, I will parse the file and use it to do updates and whatever I want. I am thinking about just doing a system call in the script like this:
my $vim = `vim $file`;
When I did this, the vi editor opened but was not visible to me. I received the following error message:
Vim: Warning: Output is not to a terminal
I will be using VIM as my editor. This all takes place in the command line. I am not sure how I should do all of this? When VIM exits, will it return a value that indicates that it was successful or a failure? Does anyone know how to do this? Thank you for your time.

Comment on How to open an editor from your Perl script?
Download Code
Re: How to open an editor from your Perl script?
by kcott (Abbot) on Jul 24, 2012 at 00:56 UTC

    Use system instead of backticks:

    ken@ganymede: ~/tmp $ cat > pm_vi_from_perl.txt Line 1 Line 2 Line 3 ken@ganymede: ~/tmp $ perl -Mstrict -Mwarnings -e 'my $f = q{./pm_vi_from_perl.txt}; syste +m(vi => $f);' ken@ganymede: ~/tmp $ cat pm_vi_from_perl.txt Line 1 Added after system() called Line 2 Here also: Added after system() called Line 3 ken@ganymede: ~/tmp $

    -- Ken

      Thank you, worked like a charm.

        The reason behind system command is ..it just spawns a different shell and runs seperately.In system you can get the exit status as 0 --> Success or Any other positive value --> ie error codes.

        Again..you can play these opening file in editors in Tk.sounds to be little complex.

Re: How to open an editor from your Perl script?
by 2teez (Priest) on Jul 24, 2012 at 01:12 UTC

    Use system function like so:

    system(qq{vim $file});
    Check the following for more info:
    1. perldoc -q backtick,
    2. perldoc -f system,
    3. perldoc -f exec

Re: How to open an editor from your Perl script?
by tobyink (Abbot) on Jul 24, 2012 at 05:53 UTC

    For what it's worth, if this is a script that you plan on sharing with others, don't hard-code vim. Some people are very particular about their choice of text editor.

    my $editor = $ENV{EDITOR} || 'vim'; system $editor => $filename;

    $ENV{EDITOR} is a very commonly used environment variable. cron uses it for editing crontabs; most VCS software (svn, git, hg, etc) uses it for editing log messages; etc.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: How to open an editor from your Perl script?
by pvaldes (Chaplain) on Jul 24, 2012 at 11:28 UTC
    "This allows the user to make changes to the file and save those changes".

    I don't feel very comfortable about this idea.

    If you open the file, dump to a var and change the var inside the perl script you can benefit of several perl features. If you call and external editor, probably not

    "Users are dumb/can't be trusted". An user could enter an error making your script totally crash or doing unexpected things i.e change the var $file to undef or even passing another little script or malware inside $file to be passed to the rest of the script. Or substitute all contents of your var $file by the word "null". Sounds like a bad idea to me.

    If a naive user can open your file, what stops her/him to use the editor to copy, reopen, move, rename, change the codification of this file instead to do what you expected?

    If you want to change a file you probably will want to use regexes. Some editors are more convenient to others for doing this, but, I can't really see the point of doing this having perl.

    There is not point into opening a very big file and then search for a point of this file if you want retrieve and change only a small paragraph/line of interest. With perl you can obtain only the lines needed, split the huge file in several chunks, pick up only the interesting columns... If something crash you could risk to loose all the file.

    What if you don't have vim? well, you fail

    You want to change a file but your editor opens the file in read-only mode for some reason. fail.

    What if you change to a computer with another operative system? fail again. Not multiplataform (without some extra-pain)

    What if vim fails to open for a unknow reason like a damaged file? you don't have the same level of info that die provides, (or is not so easily available)

    How do you deal with two files with same name when you are in a chdir different that you think?. What if you open or truncate the wrong file? Open looks safer.

    etc, etc, etc...

      I think that this cover most of your points:

      "A man who trusts nobody is apt to be the kind of man nobody trusts." -- Harold MacMillan

Re: How to open an editor from your Perl script?
by Anonymous Monk on Sep 11, 2014 at 06:11 UTC
    Hi monks, This code is working fine but is we can't use the functions of the editor since editor is running in background. So any suggestions for this problem

      Hi monks, This code is working fine but is we can't use the functions of the editor since editor is running in background. So any suggestions for this problem

      Well, that not exactly possible, so .... look for a vim option to bring the window to front

      Searching for a guitest way to bring the window to front would not be a solution I'd waste my time on, when you start editors , they usually start in the foreground ... so it should already be in the foreground ... you'd have to go out of your way to get it to start in the background, it doesn't just happen

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://983265]
Approved by GrandFather
Front-paged by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (12)
As of 2014-09-22 19:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (198 votes), past polls