kevinlang

kevinlang

How to tell if file is being executed directly

Let’s say I have a file, foo.exs with the following contents:

defmodule Foo do
  def bar do
    "foobar"
  end
end

IO.puts(Foo.bar())

What I would like to do is not run the IO.puts statement if the file is not being run directly via elixir. Is there any way to do this? I see there is __ENV__.file, but not sure how I can use that to accomplish it.

How it is done in some other languages, for example:


I also understand that using Mix and having a greater separation of concerns can solve this particular problem, but I’m curious if its possible to do without using Mix at all.

Cheers,

First Post!

Nicd

Nicd

I would rather refactor my code so that the modules that work both in a project and when run directly would be moved into .ex files, and then the code to be run directly would be in a .exs and use those modules.

Let’s say you have a foo.ex:

defmodule Foo do
  def bar(), do: IO.puts("Bar!")
end

and a test.exs just containing Foo.bar(). Now you can run elixir -pr "*.ex" test.exs and it will print Bar!.

Another option which I suggest looking into (without knowing what you are actually trying to do) are Mix tasks (I know you wanted to avoid Mix but look into it). That allows you to have tasks in your project that can be run on the command line and that can use the modules of your project. Then you can run them with mix your.task.

If you elaborate on your exact use case, then maybe we can suggest other methods.

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement