Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Tie::File Write Problem

by toolic (Bishop)
on Nov 17, 2011 at 19:30 UTC ( #938672=note: print w/replies, xml ) Need Help??

in reply to Tie::File Write Problem

After you tied the file to the @lines array, you never make a modification to the @lines array. See Tie::File.
tie @lines, 'Tie::File', $file or die "No file found to work with."; $lines[0] = 'foo'; # this should change the tied file

Replies are listed 'Best First'.
Re^2: Tie::File Write Problem
by packetstormer (Monk) on Nov 17, 2011 at 19:32 UTC
    That's my confusion, doesn't this line change the element in the line?
    if($tmp[6] eq "6 Elm") {$tmp[6] = 'XXXXX'}
    I am splitting each line up based on the commas in the line and changing that element
      That line does not change the @lines array or the tied file. That just changes the @tmp array. Tip #4 from Basic debugging checklist:
      use Data::Dumper; print Dumper(\@tmp); print Dumper(\@lines);

      @tmp is a completely separate variable that is not tied to the file. The split copies the data. You would have to add the following inside the foreach-loop to change the file as you intented:

      $i= join(',',@tmp);

      Note that even this works only because $i actually is an alias to the data in @lines and not a copy (this is magic done by the foreach loop)

        Thanks - when I think it through it makes sense as to why it wasn't writing to the file!

        Rejoining the value back into the line works perfectly!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://938672]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (15)
As of 2017-10-23 14:06 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (279 votes). Check out past polls.