By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,541 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,541 IT Pros & Developers. It's quick & easy.

how to open a text file for editing using perl

P: 2
i am working on creating a Gui environment on a Ubuntu OS.

i have a .pl file.

i need to open a /run/auto.txt file for editing in a new window, so user can edit the file manually. You know if i was using vi editor, i would have vi /run/auto.txt and it will open auto.txt in a new window so i can edit and wq! when i am done.

Below is my code and i put some comments in the code so you can see where i want my file to be open.

Any help would be greatly appreciated.




Expand|Select|Wrap|Line Numbers
  1. sub beforeDrainRecoveryNotes
  2. {
  3.   $file = "$runDir/auto.txt";
  4.   while((my $key, my $value) = each %stationSelected)
  5.   {
  6.     if($value)
  7.     {
  8.  
  9.       if(my $pid = fork) { $pids{$pid}++; }
  10.       else
  11.       {
  12.       #  &runSysComm("$binDir/win $key");
  13.       #   This is where i want to open the auto.txt file in 
  14.       #   new window so user can edit this file. 
  15.       #   After editing is done, user will save and close the file
  16.       #   preferbally using wq!      
  17.  
  18.         CORE::exit();
  19.       }
  20.     }
  21.   }
  22.   printErr();
  23. }
May 11 '10 #1
Share this Question
Share on Google+
5 Replies


Expert Mod 100+
P: 589
Why should the user use "wq" to save and close the file?

If you're creating a GUI, then you should have a menubar with a File dropdown like all other GUI's.

For editing the file, you need to use Perl's open function.
See: http://perldoc.perl.org/functions/open.html
May 11 '10 #2

numberwhun
Expert Mod 2.5K+
P: 3,503
@RonB He mentioned that that is what he does when editing with vi. That means that he probably has vi aliased to gvim (Graphical Vim), which still accepts the :wq to save and exit a file, but also has the "File" menu you mentioned.

Us Unix command line geeks still like our keyboard interface, even in the face of a gui. Its a nice added feature. :)
May 11 '10 #3

Expert Mod 100+
P: 589
Jeff,

Ya, I knew that, I just wanted the OP to clarify what (s)he wants. I just made the same mistake, which was not making it clear what I was asking.

So, (usustarr) is this a Perl Tk GUI script, or are you wanting to use Perl to drive gvim? I've never tried, but it's probably possible to drive gvim, but that would be IMO the worst possible approach.
May 11 '10 #4

P: 2
@RonB
this piece of the code came from Gui environment.
1) i want to open this text file in a new window so a user (human) can edit. That part of the code needs to be located in side of that else statement.

2) when user done with editing i need a way to save and close this new window we opened. i prefer wq!, but i am open to any other ideas too.

i am new to Perl,. so i am having hard time with this. i know how to open a file for editing if editing was done by a script. Can you please help me out here?
May 20 '10 #5

Expert Mod 100+
P: 589
We still seem to be dancing around the root of what you want, which means I don't have a clear picture of what you really want.

1) i want to open this text file in a new window so a user (human) can edit. That part of the code needs to be located in side of that else statement.
That tells me that you simply want Perl to launch gvim to edit your file.

2) when user done with editing i need a way to save and close this new window we opened. i prefer wq!, but i am open to any other ideas too.
That tells me that you want to use one of Perl's gui frameworks (possibly Perl/Tk) to create a gui that allows the user to modify the file and uses some of the (gvim) keyboard shortcuts to edit/save the file.

Those 2 approaches have entirely different solutions and requires completely different code base. Which do you want to do?

The ability to clearly state what you're doing and what you need will go a long way in us being able to suggest solutions.
May 21 '10 #6

Post your reply

Sign in to post your reply or Sign up for a free account.