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
For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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., 2023: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho