Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems

Phone System reconfiguration 11.1.2018 and 12.1.2018 - we are on email but incoming phone not available all the time
modifying files..

Posted by kitwalker2886 (kitwalker2886), 21 May 2007
hi..its me again.. tell me something.. how can we modify files using perl.?? as in.. i want to open a file.. read the content and modify a part of it.. and store it back in that same file.
   and also.. can i create a new directory and then write to files in it from the program. i tried using mkdir,open dir n then open.. but dint work.

Posted by admin (Graham Ellis), 21 May 2007
You could modify a file "in situ" if you are certain that you'll be replacing a number of bytes with exactly the same number of bytes, but otherwise you'll need to read the old file completely then writ eback to it of (if it's a big file) do a read and write loop, followed by a rename.   Discs aren't like trains where you can always squeeze one more on - once yiou have 512 bytes in a secrot, you can't squeeze any more in!

If you have appropriate permissions, you can create directories, files, etc, from Perl - not a problem at all.   If it's not working, have a look at the special variables such as $@, $? and $! to find out why.

See our more about files and advanced fiel and directory handling pages for many source code examples

Posted by kitwalker2886 (kitwalker2886), 23 May 2007
yeah.. i did that reading and renaming thing that u suggested.. could u tell me a bit more bout modifying them insitu if i m sure i ll be replacing the same number of bytes.. ?? Is there a way to open a file in both read and write mode like in c++..?

Posted by admin (Graham Ellis), 23 May 2007
Yes - the examples in the "advanced file handling" section I linked to in my previous post should help.

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho