How do you deal with pids, ports, and refs in iex?

In erl shell I can paste <x.y.z> and it would be parsed as a PID. From

The shell can parse commands with pids (<0.60.0>), ports (#Port<0.536>), references (#Ref<0.1662103692.2407792644.214210>), and external functions (#Fun<a.b.1>), but the command fails unless the corresponding pid, port, reference, or function can be created in the running system.

But in iex I seem to have to use pid/1,3, ref/1,4 functions, and I don’t know how to deal with ports (currently I use Port.list/0 but there’s probably a nicer approach) … Does anyone know if there is any format for pids, refs, ports, etc that iex can parse?

1 Like
╰─➤  iex
Erlang/OTP 21 [erts-10.1.1] [source] [64-bit] [smp:6:6] [ds:6:6:10] [async-threads:1] [hipe]

Interactive Elixir (1.7.4) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> h pid

                                def pid(string)                                 

Creates a PID from string.

## Examples

    iex> pid("0.21.32")

                                def pid(x, y, z)                                

Creates a PID with 3 non-negative integers passed as arguments to the function.

## Examples

    iex> pid(0, 21, 32)
    iex> pid(0, 64, 2048)

That’s the format it uses. :slight_smile:

A Port is just a PID for note.

1 Like

So there is no way to use something like <0.21.32> to get #PID<0.21.32>? That is, without having to use a function? Just copy-pasting #PID<0.21.32> back into the iex would be great.

A Port is just a PID for note.

Thanks! But how can I use it? I see ports as [#Port<0.0>, #Port<0.2>, #Port<0.4>], and I can’t use pid(0, 0) or pid("0.0").

I think ports are considered ‘external pids’ (like pids on a remote system), so you can’t use the pid functions on those. Why not assign it to something? :slight_smile:

Why not assign it to something? :slight_smile:

Sometimes I just don’t have this ability, so I default to Port.list/0 which is a bit annoying if there are many open ports. Unless the approach I showed above with copy-pasting is impossible due to some inner workings of iex,

So there is no way to use something like <0.21.32> to get #PID<0.21.32> ? That is, without having to use a function? Just copy-pasting #PID<0.21.32> back into the iex would be great.

something like port/2 (similar to ref/4 and pid/3) would be nice to have.

1 Like

I think it would be possible to make such a function, wouldn’t be perfectly performant but if run from iex that wouldn’t really matter, just add something like def port(a, b), do: Enum.find(Port.list(), &(inspect(&1) == "#Port<#{a}.#{b}>")) to your iex helpers, like in a .iex.exs file that contains something like:

def IExHelpers do # Support both string and integral representations
  def port(i), do: Enum.find(Port.list(), &(inspect(&1) == i))
  def port(a, b), do: port("#Port<#{a}.#{b}>")
import IExHelpers
1 Like

The way I do this is to store the last fews transaction (e.g. in Phoenix or Plug) into a ConCache, and then I can retrieve the original item from Cache. For long-running things, I pull them out of the Application.env or possibly a given GenServer state. It’s almost always for debugging, so the other option is to use tracing to collect the raw data as either function call parameters going in, or as return values on the way back.

1 Like

In my case I look up the ports with :sys.get_state, then use Port.list to get the position of the port, and then to actually get the port … @OvermindDL1’s approach would save me one step, but still, just copy pasting #Port<0.2043> from :sys.get_state output and into iex would be great. What I wonder about is if there is a way to make iex recognize #Port<...>, #PID<...>, and #Reference<...> as not comments, but port, pid, and reference respectively.

1 Like

As @michalmuskala wrote in a feature request we can currently do:

iex> :erlang.list_to_port('#Port<0.7>')