Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Update works for line...deletes other records

by PilotinControl (Pilgrim)
on Feb 21, 2013 at 04:15 UTC ( #1019881=perlquestion: print w/replies, xml ) Need Help??
PilotinControl has asked for the wisdom of the Perl Monks concerning the following question:

my code below works fine updating the line in a file however it deletes all other records...not sure why? Thanks in advance for the solution to the code below.

sub editgenord { print colored ("| ORIGINAL DATE: |\n\n",'green'); my $origgenordDate = <STDIN>; chomp $origgenordDate; print colored ("| REVISED DATE: |\n\n",'green'); my $revgenordDate = <STDIN>; chomp $revgenordDate; my $file = "generalorderdata.txt"; local $^I = ".bak"; local @ARGV = ($file); while (<>) { chomp; my ($origDate,$gnOrd,$revDate) = split(/\:/); if ($origDate eq $origgenordDate) { print "$origDate:$gnOrd:$revgenordDate\n"; } } unlink("$file.bak"); close $file; print colored ("GENERAL ORDER UPDATED SUCCESSFULLY!!\n",'green'); sleep 3; genord(); } # END UPDATE GENERAL ORDER

Replies are listed 'Best First'.
Re: Update works for line...deletes other records
by Anonymous Monk on Feb 21, 2013 at 04:53 UTC
Re: Update works for line...deletes other records
by tmharish (Friar) on Feb 21, 2013 at 06:16 UTC
Re: Update works for line...deletes other records
by 7stud (Deacon) on Feb 21, 2013 at 07:52 UTC

    From 'man perlrun':

                specifies that files processed by the "<>" construct are to be
                edited in-place.  It does this by renaming the input file, opening
                the output file by the original name, and selecting that output
                file as the default for print() statements. 

    Okay, so once perl renames the original file, the original file name doesn't exist anymore. Subsequently, when perl opens a file name that doesn't exist for writing, what's in that file to start?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1019881]
Approved by vinoth.ree
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2018-05-24 16:11 GMT
Find Nodes?
    Voting Booth?