How to do File I/O in Opa?
Asked Answered
D

2

5

After reading (nearly) the whole ebook and taking a look at the API
i am still asking myself how to realize "traditional" web server behaviour with opa.

I understand (at least i believe that) that opa links external resources specified at
compile time into the executable, making them immutable and permament.

But what if, say, i would want to change the stylesheet of an application without recompiling it?
There seems to be a few methods in the stdlib (apidoc) but they are not covering what i am used to from other programming languages.

A possible solution i could think of is making use of the internal database,
but that looks like a bit of an overkill for something simple like traditional File I/O.

Distinguish answered 5/7, 2011 at 17:59 Comment(0)
B
6

Edit: this blog post explains more about dealing with external resources in Opa.


Long story short: you'll rarely work with external files in Opa.

Let me try to break this down. Opa will indeed embed resources. But for development mode you indeed just want to be able to tweak them (mainly CSS) and see changes immediately. If you compiled your program in a non-release mode then it will support this kind of actions (try --help, below is an excerpt)

Debugging Resources : dynamic edition:
    [...]
    --debug-editable-css
                Export the CSS files embedded in the server to the file
                system, so that they can be viewed and edited during
                execution of the application

For many other editable&changing resources one would indede use the database.

And if you really need to work with files (again: with Opa you'll need it much less than with traditional web languages) then take a look at stdlib.io and, for advanced use, at BslFile module with bindings to Ocaml functions for file manipulation.

Balsa answered 5/7, 2011 at 18:17 Comment(2)
Thank you very much akoprowski, i did not know about the debugging modes. I will take a close look at that!Distinguish
Ok, enjoy it :). Usually if you want to thank for an answer on StackOverflow you can up-vote it and/or select it as the answer you your question :)Balsa
P
2

I think this module is for you : http://opalang.org/resources/doc/index.html#file.opa.html/!/value_stdlib.io

import stdlib.io
my_css = File.content("css/file.css")

I am not seeing some way to write file, but I think if you need to write you should use the db. But to read I think this is the solution :)

Priestridden answered 5/7, 2011 at 18:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.