As you have noted, this does not change the file in place. You have two possible approaches here :

  1. Create a second file instead of personel.txt, and overwrite personel.txt at the end of your program
  2. Use the Tie::File module to treat your file as an array
The first approach will need almost no modification of your script and only the addition of the rename code to the end of your script. The second approach will need some modification of your script, but after that, your script will also run on a system that does not have grep installed. An example for how the second approach could look is :

#!/usr/bin/perl -w use strict; use Tie::File; my $newdigit="+615"; my $search_id="0001"; my $personel_filename; my @personel; tie @personel, 'Tie::File', File::Spec->catfile($FindBin::Bin,$persone +l_filename) or die "Couldn't read '$personel_filename'\n"; for my $line (@personel) { my ($id, $name, $family, $oldphone)=split(/\|/, $line); # Look if we want to replace the phone if ($id eq $search_id) { $newphone = $newdigit .$oldphone; # And overwrite the line in the array $line = join ("|",$id, $name, $family, $newphone); }; };

In reply to Re: Substitue! by Corion
in thread Substitution of text within a file by Anonymous Monk

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.