root/examples/vmethods/escape.pl

Revision 11293, 0.9 kB (checked in by Darren_Duncan, 3 years ago)

replaced all 'use v6;' lines with 'use v6-alpha;' in 330 files (examples/, ext/, t/, t_disabled/) ... more remain to do

  • Property svn:mime-type set to text/plain; charset=UTF-8
  • Property svn:eol-style set to native
Line 
1# Please remember to update examples/output/vmethods/escape if you change this
2# file so its output will change.
3
4use v6-alpha;
5
6multi sub escape (Str $source is copy, Str $lang) {
7  given $lang {
8    when "html" {
9      $source ~~ s:Perl5:g/([&<>"'-])/{ #"#--vim
10        $0 eq "&" ?? "&amp;"  !!
11        $0 eq "<" ?? "&lt;"   !!
12        $0 eq ">" ?? "&gt;"   !!
13        $0 eq '"' ?? "&quot;" !!
14        $0 eq "'" ?? "&#39;"  !!
15        $0 eq "-" ?? "&#45;"  !! die
16      }/;
17    }
18
19    when "doublequote" {
20      $source ~~ s:Perl5:g/([\\"])/{
21        $0 eq "\\" ?? "\\\\" !!
22        $0 eq "\"" ?? "\\\"" !! die
23      }/;
24    }
25
26    default {
27      die '"lang" parameter must be "html"|"doublequote"' unless
28        $lang eq any <html doublequote>;
29    }
30  }
31
32  return $source;
33}
34
35say 'This string should be quoted: "\\<>&-"'.escape(:lang<html>);
36say 'This string should be quoted: "\\<>&-"'.escape(:lang<doublequote>);
Note: See TracBrowser for help on using the browser.