Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Perl allows you to choose your own quote delimiters. If you find you need to use a double quote inside of an interpolating string (i.e. <code>""</code>) or single quote inside of a non-interpolating string (i.e. <code>''</code>) you can use a <a href="http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators" rel="noreferrer">quote operator</a> to specify a different character to act as the delimiter for the string. Delimiters come in two forms: bracketed and unbracketed. Bracketed delimiters have different beginning and ending characters: <code>[]</code>, <code>{}</code>, <code>()</code>, <code>[]</code>, and <code>&lt;&gt;</code>. All other characters<code>*</code> are available as unbracketed delimiters.</p> <p>So your example could be written as</p> <pre><code>$foo = qq(12."bar bar bar"|three); </code></pre> <p>Inserting text after "12." can be done many ways (<a href="http://en.wikipedia.org/wiki/There_is_more_than_one_way_to_do_it" rel="noreferrer">TIMTOWDI</a>). A common solution is to use a <a href="http://perldoc.perl.org/perlop.html#s/PATTERN/REPLACEMENT/msixpogce" rel="noreferrer">substitution</a> to match the text you want to replace.</p> <pre><code>$foo =~ s/^(12[.])/$1../; </code></pre> <p>the ^ means match at the start of the sting, the <code>()</code> means capture this text to the variable <code>$1</code>, the <code>12</code> just matches the string <code>"12"</code>, and the <code>[]</code> mean match any one of the characters inside the brackets. The brackets are being used because <code>.</code> has special meaning in regexes in general, but not inside a character class (the <code>[]</code>). Another option to the character class is to escape the special meaning of <code>.</code> with <code>\</code>, but many people find that to be uglier than the character class.</p> <pre><code>$foo =~ s/^(12\.)/$1../; </code></pre> <p>Another way to insert text into a string is to assign the value to a call to <a href="http://perldoc.perl.org/functions/substr.html" rel="noreferrer"><code>substr</code></a>. This highlights one of Perl's fairly unique features: many of its functions can act as lvalues. That is they can be treated like variables.</p> <pre><code>substr($foo, 3, 0) = ".."; </code></pre> <p>If you did not already know where <code>"12."</code> exists in the string you could use <a href="http://perldoc.perl.org/functions/index.html" rel="noreferrer"><code>index</code></a> to find where it starts, <a href="http://perldoc.perl.org/functions/length.html" rel="noreferrer"><code>length</code></a> to find out how long "12." is, and then use that information with <code>substr</code>.</p> <p>Here is a fully functional Perl script that contains the code above.</p> <pre><code>#!/usr/bin/perl use strict; use warnings; my $foo = my $bar = qq(12."bar bar bar"|three); $foo =~ s/(12[.])/$1../; my $i = index($bar, "12.") + length "12."; substr($bar, $i, 0) = ".."; print "foo is $foo\nbar is $bar\n"; </code></pre> <p><code>*</code> all characters except whitespace characters (space, tab, carriage return, line feed, vertical tab, and formfeed) that is</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload