Always group reply so the list can help as well, as I definitely don't have all the answers :-).

The code I sent earlier will open the list for reading only. To open a file for writing you would have to use the > operator in the open, or >> for appending, check out perldoc -f open for more info on this.

In general I would probably open the file, read its contents, and then close it, then open it again if I was doing anything but appends, but that is in general slower as it requires two opens, but it is probably more a matter of preference. Check out the perldoc -f open doc page mentioned earlier for a lot of information about how to do various opens, and you may also want to check out perldoc perlopentut. You may also want to consider a backup plan, once you start writing your file if something happens to the execution of the program whatever hasn't been written will be lost, so you should write to a temporary file and then move it into place, or something similar so that you have a way to recover the contents in the event of a meltdown.

http://danconia.org


Ebaad Ahmed wrote:


Hi Wiggins,

Thanks a lot for responding to my message, I really liked the idea of openeing a separate file and storing the elements in there, but I just have one question that will this open the file for reading and writing both or do I have to open it again for writting. Please forgive me if it is a stupid question. If you want I can email you the code, I only wanted to store the contents of a scroll list in a file so that I dont have to edit the code in order to add or delete a name from the list.

Regards,
Ebaad.
>From: Wiggins d'Anconia
>To: Ebaad Ahmed
>CC: [EMAIL PROTECTED]
>Subject: Re: including data from an external file
>Date: Wed, 01 Jan 2003 20:39:26 -0500
>
>I am not entirely sure I get what you are trying to do, but see
>inline.
>
>Ebaad Ahmed wrote:
>>Hello everybody,
>>
>>I would like to include a file in my cgi code so that it contains
>>all the lists of data, and later can be edited from another form
>>to create an admin tool for the lists in the main form.
>>
>
>If you are just talking about a list of data, then it can be stored
>either as actual perl code or as a just text delimited by something
>(most likely new lines). In the case of using perl code, you could
>just declare it as an array like in any script, note that it will
>have to be a global which would make strict complain and this is not
>a good way to do anything but a one-off.
>
>
>>I have researched alot and found that I can use "require" or "use"
>>for this purpose
>>but I still could not find a complete simple example of how to
>>declare the array in an external file and how to recall the data
>>from the cgi script in order to display it.
>>
>
>If you still want to take this approach of having the data stored as
>a perl structure the easiest way would be to store it lexically in
>the file as an array for instance, then write a quick subroutine to
>return either the array itself, or a reference to the array. For
>example:
>
>our @data = ('element1','element2','element3');
>
>sub return_data {
> return @data;
>}
>
>or
>
>sub return_data_as_array_ref {
> return \@data;
>}
>
>And then in your other script you would simply say:
>
>require 'data_file.pl';
>@local_copy_of_data = return_data();
>
>or
>
>$local_ref_to_data = return_data_as_array_ref();
>
>
>>Any help will be greatly appreciated.
>>
>
>However if it is just a list of data, you might consider just
>storing it one element per line, then using 'open' to open the file
>and then read it into an array or line by line.
>
>my $HANDLE;
>open($HANDLE,'/path/to/file_with_data.txt') or die "Can't open data
>file: $!";
>my @data = <$HANDLE>;
>close($HANDLE);
>
>In both cases to edit the file you will have to do an open and the
>print the contents into the file either manually doing your
>formatting if any is required or using something like Data::Dumper
>or XML, etc.
>
>good luck,
>
>http://danconia.org
>
>
>--
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]

------------------------------------------------------------------------
Protect your PC - Click here <http://g.msn.com/8HMOEN/2024> for McAfee.com VirusScan Online

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to