elixir-book/basic-types/with-scopes.exs

15 lines
480 B
Elixir

content = "Now is the time"
lp =
# user pattern machting to open file and assign it to variable
with {:ok, file} = File.open("/etc/passwd"),
content = IO.read(file, :all),
:ok = File.close(file),
# use regex to find uid and guid. MatchError, if there's no such user.
[_, uid, gid] = Regex.run(~r/^root:.*?:(\d+):(\d+)/m, content) do
"Group: #{gid}, User: #{uid}"
end
# => Group: 26, User: 26
IO.puts(lp)
# => Now is the time
IO.puts(content)