# Check something only once on function call

**URL:** <https://discourse.julialang.org/t/check-something-only-once-on-function-call/13360>\
**Category:** General Usage\
**Tags:** development\
**Created:** [August 13, 2018, 11:50am UTC](https://discourse.julialang.org/t/check-something-only-once-on-function-call/13360 "2018-08-13T11:50:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [August 13, 2018, 11:50am UTC](https://discourse.julialang.org/t/check-something-only-once-on-function-call/13360/1 "2018-08-13T11:50:37Z")

</div>

I have a nice function

```julia
const MUSESCORE = @static Sys.iswindows() ? "MuseScore" : "musescore"

function test_musescore()
    r = try
        r = run(`$(MUSESCORE) -v`)
    catch
        false
    end
    if r == false || ((typeof(r) == Base.Process) && r.exitcode != 0)
        throw(SystemError(
        """
        The command `$(MUSESCORE) -v` did not run, which probably means that
        MuseScore is not accessible from the command line. Please first install MuseScore
        on your computer and then add it to your PATH."""
        ))
    end
end

```

inside a julia package. This function is called later on to confirm that another function can actually run, e.g.

```julia
function musescore(file, notes; display = true, grid = nothing)

    test_musescore()
    # do stuff ...
end

```

Now here is my question:

I want to be able to run `test_musescore()` **only once** from within `musescore` per new session a user starts. Is this possible? Because I think try blocks are slow, if one wants to do “Batch conversions” that may be bad for performance. So how can this be done in a performant way? via a global variable?

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [August 13, 2018, 12:02pm UTC](https://discourse.julialang.org/t/check-something-only-once-on-function-call/13360/2 "2018-08-13T12:02:56Z")

</div>

Not really answering your question directly, but is `try`ing the command the only way to figure out if it’s there? I’m not familiar with Windows command line stuff, but with Unix you could do something like `which musescore` which would return a blank line if the program isn’t in the $PATH, or return the path if it is.

I’m not sure best practices with global variables, but I often make a `const FLAGS = Dict{Symbol,Bool}()`. I haven’t benchmarked anything, but it seems fine (prsumably the type of anything pulled from the dict can be known, but the dict itself is mutable so I can change or add flags).

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [August 13, 2018, 12:05pm UTC](https://discourse.julialang.org/t/check-something-only-once-on-function-call/13360/3 "2018-08-13T12:05:24Z")

</div>

To be honest I am _totally clueless_ about how to check whether something is on the command line 😃 so if anyone has a better suggestion please be my guest.

The dictionary suggestion seems okay. I also am not sure about the performance due to the global nature but maybe it is not that bad in the end. Most of the time will be spent out of Julia anyway.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [August 13, 2018, 12:15pm UTC](https://discourse.julialang.org/t/check-something-only-once-on-function-call/13360/4 "2018-08-13T12:15:07Z")

</div>

> [@Datseris](#):
>
> To be honest I am _totally clueless_ about how to check whether something is on the command line 😃 so if anyone has a better suggestion please be my guest.

See if `which` works:

```julia
$ which musescore
$ which python
/usr/local/bin/python

```

So you could just check if the return has length 0. Again, this is unix, not sure about windows equivalent.

As to the Dict, I remember reading that using `const` helps with the performance of global stuff because the compiler knows it can’t change. Again, I haven’t interrogated this extensively and might be misremembering.

---

<div class="post-metadata">

**Author:** ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)\
**Post date:** [August 13, 2018, 12:16pm UTC](https://discourse.julialang.org/t/check-something-only-once-on-function-call/13360/5 "2018-08-13T12:16:14Z")

</div>

`which` doesn’t exist on windows, yes. So I am looking for a platform independent way of checking this…
