Hacker News new | comments | show | ask | jobs | submit login

in perl 6 you can quote strings in quite a few ways!

https://docs.perl6.org/language/quoting

"Quoting" is all defined inside of a sub-language called "Q"




Ruby has pretty much the same flexibility. "%" followed by almost any non-alphabetic character starts a quoted string. But most then become the quote character. If the quote character is at least <,(,[ or { then the end-quote character is the corresponding >,),] or }. These are all equivalent:

%<x>

%"x"

%{x}

%!x!

Unfortunately the rules are loose enough that even space is a valid quote character. So this too is equivalent to the above:

% x

("% x ")


No it doesn't have the same flexibility.

Perl 6's string quoting sub language has various feature flags that you can turn on or off.

Here are a few of the basic ones shown in valid code (comments and newlines included)

  # start quoting with no features enabled
  # (not even backslashing the delimiter is enabled)
  Q

    :scalar    # enable $foo
    :array     # enable @foo[]
    :hash      # enable %foo{}
    :closure   # enable { 12/3 }
    :function  # enable &foo('bar')
    :single    # turns on backslashing the delimiter
    :backslash # enable \n
  
    :!exec     # turn off executing it (redundant)
    :exec(0)   # another syntax for turning off a feature
  
  {…}          # various delimiters are allowed (generally punctuation)
There are shorter variants of each of the feature flags. :c => :closure

I would like to point out that the parsing of the :closure and :function part of the sub language is reentrant. (Actually most of them add some form of reentrancy, it is just harder to show it for the others)

  "a b &foo( "c d &bar( "baz" )" )"
Note that this reuses the base Perl 6 parser, and that is why it is reentrant.

There are shortcuts for regularly used forms

  「」 =:= Q[]
  
  '' =:=  q[] =:= Q:q[]  =:= Q:single[]
  
  "" =:= qq[] =:= Q:qq[] =:= Q:double[]
     =:= Q:b:s:a:h:c:f[]
     =:= Q:backslash:scalar:array:hash:closure:function[]
The ability to turn off features can be useful

  qq :!c [a {\n  b\n} c] # { and } don't form a closure here
I would like to point out that the :foo syntax is used everywhere in the language for named parameters to routines and operators (most operators are implemented as subroutines)

  :foo =:= :foo( True ) =:= foo => True
  
  :!bar =:= :bar( False ) =:= bar => False
  
  :$baz =:= :baz( $baz ) =:= baz => $baz
If the delimiters are paired () <> {} 「」 «» “” ‘’ you can double up on them.

  q<FOO> =:= q<<FOO>> =:= q<<<<<FOO>>>>>
This is useful to avoid having to backslash a delimiter within the string, or trying to find a delimiter that isn't in the string.

  q<< <a> >> =:= q' <a> '


One more equivalent expression: ?x

A question mark followed by any other single character is treated as a string literal containing that character.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: