Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Creating file with open(PUB,"+</home/$NAME");

Posted on 1999-07-03
14
Medium Priority
?
256 Views
Last Modified: 2010-03-04
The following should create a file if there isnt one.
                            But it doesnt. What is wrong with this code?

                            open(PUB,"+</home/$NAME");
                            flock(PUB, 2);
                            @plines = <PUB>;
                            seek(PUB, 0, 0);
                            push(@plines,"$msgcnt $tempfr");
                            print PUB @plines;
                            flock(PUB, 8);
                            close(PUB);
0
Comment
Question by:Karina051699
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 

Author Comment

by:Karina051699
ID: 1213783
Comment: ozo
                                                                  Date: Sunday, July 04 1999 - 01:52AM SAMST

            open(PUB,"+</home/$NAME") || die "Can't open '/home/$NAME' for update: $!";
            #or did yoy mean open(PUB,"+>>/home/$NAME") #to create a file if there isnt one?

            #also, prior to 5.004, which did an automatic flush,
            #a close after a LOCK_UN could write the file after someone else had obtained a lock.
0
 

Author Comment

by:Karina051699
ID: 1213784
Yes, it should create a file if there isnt one or overwrite the outdated


open(PUB,"+</home/$NAME") ||
open(PUB,"+>/home/$NAME") ||
open(PUB,"+>>/home/$NAME") ||
wont!

open(PUB,">$file_d/vis$room");
close(PUB); does!!
      Sorry, I posted a duplication of the question, and removed the one with your comment in

My PERL is 5.005
0
 
LVL 84

Expert Comment

by:ozo
ID: 1213785
So what did $! say?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Karina051699
ID: 1213786
It returns a.." no such file or directory"

(path is correct and 777)
0
 

Expert Comment

by:n1875621
ID: 1213787

does changing the first line to

open(PUB,"+</home/" . $NAME);

help?
0
 

Author Comment

by:Karina051699
ID: 1213788
Unfortunately not! :( same error


open(PUB,"+>/home/$NAME") || die "Can't open '/home$NAME' for update: $!";

creates the file but does not read in the data if the file exists. (it just overwrites with
the pushed line)
0
 
LVL 84

Expert Comment

by:ozo
ID: 1213789
#it still looks like you wanted to do
open(PUB,"+>>/home/$NAME") || die "Can't open '/home/$NAME' for append update: $!";
#or
open(PUB,">>/home/$NAME") || die "Can't open '/home/$NAME' for append write: $!";
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1213790
if you want to append to a file you should use the >> and not +> statement in the open function...

take this for example...

open(hFile, ">>append.txt");
print hFile "APPEND";
close(hFile);

this will create the file if it's not there already, but if it is it will simply append the text to it or whatever..

..-=ViKtOr=-..
0
 

Author Comment

by:Karina051699
ID: 1213791
+>> this just appends!!


1.  it should read in the data if the file exists, if not it should create the file
2 .code sorts and shortens the number of lines.. (@plines = @plines[(0 .. 5)];)
3 the old data in the file is no longer needed!

+< works fine, but it does not create one if there isnt one
0
 

Author Comment

by:Karina051699
ID: 1213792
i dont want to have the script to append data to a file. it should completely overwrite
the file with the new sorted datalines.

+< does this, but it just writes to the file if this filename already exists
0
 
LVL 84

Expert Comment

by:ozo
ID: 1213793
#appending is what your example seemed to be doing,
#but if you want to completely overwrite:
open(PUB,"+>>/home/$NAME") || die $!;
flock(PUB,LOCK_EX);
open(TEMP,">/home/$NAME.$$") || die $!;
@plines = <PUB>;
@plines = sort @plines;
print TEMP @plines;
close TEMP;
rename "/home/$NAME.$$", "/home/$NAME" or die $!;
close PUB;
# see also the $^I switch
0
 

Author Comment

by:Karina051699
ID: 1213794
Ozo thanks, I accept your answer, please reply with one to grade it.

Please just let me know, if there is no way to create a file with +<, would it
not be more efficient with the following? The script is called a few hundert
time per minute and I would like to have it with as less as possible disk
operations (best would be a single open/close, without rename)



 if(!-e "/home/$NAME") {
  open(PUB,">/home/$NAME");
  close(PUB);
 }
open(PUB,"+</home/$NAME");
flock(PUB, 2);
@plines = <PUB>;
seek(PUB, 0, 0);
push(@plines,"$msgcnt $tempfr");
print PUB @plines;
flock(PUB, 8);
close(PUB);
0
 
LVL 84

Expert Comment

by:ozo
ID: 1213795
if(!-e "/home/$NAME") {
#At this point, if someone else creates the file and writes to it,
#it will get written over by the following
      open(PUB,">/home/$NAME");
       close(PUB);
}


But if you want to use a separate file for the flock,
then you might just let
 @plines=();
 @plines = <PUB> if open(PUB,"</home/$NAME");
fail, since @plines would just start empty,
and the file would get created by the rename
0
 
LVL 2

Accepted Solution

by:
djsansui earned 300 total points
ID: 1213796
When you use
open(PUB,"+</home/$NAME");
it only opens existing files, not create new ones

open(PUB,"+>/home/$NAME");
should destroy any existing file, and create a new one

The problem may be in your path name, you say that
open(PUB,">$file_d/vis$room");  works but
open(PUB,"+</home/$NAME"); doesn't

perl doesn't always like a leading slash, it doesn't use the same path as usual.  if you are trying to use a directory outside of the cgi-bin, you must use the full path within your server to get to your page, such as "/member/www/home/$NAME", rather than just "/home/$NAME".

That's your answer, for a lengthy explanation, read on.

This is because the structure on the server is often different than the structure of your web page, the cgi-bin is often aliased to another address so that it is easier to access.  In your base dir, you may have both a 'www' dir (where your web files go) and a 'cgi-bin' dir, this obviously won't work, because the cgi-bin is called as if it were IN the 'www' dir.  This has to do with how the server is set up.

If you're familiar with Apache (or any other webserver) you'll probably know what I'm talking about.  You can't create files outside of your base www directory, and this is probably the problem you're running into.

I hope this solves your problem.
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question