Escape closed parenthesis in elixir sigil

718 Views Asked by At

I want to have a custom sigil, so that i can have one element per line.

This is code i have

def sigil_l(text,[]), do: String.split(text,"\n")

This works fine for

~l(Clash of Titans
   The day after Tomorrow
   The Transporter
 )

This fails for

~l(The man from Earth (2007)
   Gone Girl (2014)
   )

Notice the brackets above

This is the error message

"{" starting at line 38 is missing terminator "}". Unexpected token: )
(elixir) lib/kernel/parallel_compiler.ex:97: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/8

The Expected outcome is

["The man from Earth (2007)",
"Gone Girl (2014)"]

What code needs to be changed. Do i need to add any characters in input as well and handle in sigil defination ?

Update

The solution that @AbM gave is correct. I was on elixir version 1.0.4 so it did not work. It does works on 1.1.x

Solution

~l(The man from Earth (2007\)
   Gone Girl (2014\)
   )
2

There are 2 best solutions below

4
On BEST ANSWER

You should escape the closing parenthesis with \):

~l(The man from Earth (2007\)
   Gone Girl (2014\)
)

Here's my script and iex output:

defmodule SigilL do

  def sigil_l(text,[]), do: String.split(text,"\n")

end

enter image description here

0
On

There are also a variety of delimiters for sigils. So instead of:

~s(My funky "string(\)")

You can also use:

~s|My funky "string()"|

Which avoids the use of a forward slash. Obviously, that means you'll have to find another delimiter for:

~s|My funky "string(|)"|

But at least there's a solution available for that. https://elixirschool.com/en/lessons/basics/sigils/