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

Re: opened file being overwritten

by Anonymous Monk
on Dec 13, 2012 at 21:05 UTC ( [id://1008726]=note: print w/replies, xml ) Need Help??


in reply to opened file being overwritten

Why are you opening all the three files before and reading from the 4th file? Also note that that '<' is read and '>' is write mode in the open function.

Replies are listed 'Best First'.
Re^2: opened file being overwritten
by perishowl (Initiate) on Dec 13, 2012 at 21:17 UTC
    #!/bin/Perl use warnings; if (@ARGV < 4) { print ("you need to provide 4 arguments\n\t4th argument is file to + parse"); exit; } $fh = pop(@ARGV); openfile(@ARGV); sub openfile { open FILE1,">$_[0]" || die "could not open $_[0]"; open FILE2,">$_[1]" || die "could not open $_[1]"; open FILE3,">$_[2]" || die "could not open $_[2]"; open FILE4,"<$fh" || die "could not open $fh"; while (<FILE4>) { print $_; } }

    this makes no sense why the FILE4 file handle wont be printed i opened it for reading

      from open,

      If MODE is ">", the file is opened for output, with existing files first being truncated ("clobbered") and nonexisting files newly created. If MODE is ">>", the file is opened for appending, again being created if necessary.
      So, your files $_[0] to $_[3], is been "clobbered", by the ">" mode, while the last one should print out.

      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me

      Another large bug in your code is that due to operator precedence, your dies won't work.

      Basically, open FILE1,">$_[0]" || die "could not open $_[0]" is being interpreted as open FILE1,(">$_[0]" || die "could not open $_[0]")

      $ perl -MO=Deparse -e 'open INP, "<", "hello" || die $!' + open INP, '<', 'hello'; -e syntax OK $ perl -MO=Deparse -e 'open(INP, "<", "hello") || die $!' + die $! unless open INP, '<', 'hello'; -e syntax OK $ perl -MO=Deparse -e 'open INP, "<", "hello" or die "could not open: +$!"' die $! unless open INP, '<', 'hello'; -e syntax OK

      You either need to use the lower-precedence or operator or call the open() function with parentheses.

Re^2: opened file being overwritten
by perishowl (Initiate) on Dec 13, 2012 at 21:13 UTC
    the code isnt finished, im planning on writing data to the other 3 files

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1008726]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-29 13:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found