Search code examples
elixirsigils

Escape closed parenthesis in elixir sigil


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\)
   )

Solution

  • 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