Printing string in Perl

18,358

Solution 1

perldoc perlop, under "Quote and Quote-like Operators", contains everything you need.

While we usually think of quotes as literal values, in Perl they function as operators, providing various kinds of interpolating and pattern matching capabilities. Perl provides customary quote characters for these behaviors, but also provides a way for you to choose your quote character for any of them. In the following table, a "{}" represents any pair of delimiters you choose.

Customary  Generic        Meaning        Interpolates
    ''       q{}          Literal             no
    ""      qq{}          Literal             yes
    ``      qx{}          Command             yes*
            qw{}         Word list            no
    //       m{}       Pattern match          yes*
            qr{}          Pattern             yes*
             s{}{}      Substitution          yes*
            tr{}{}    Transliteration         no (but see below)
    <<EOF                 here-doc            yes*

    * unless the delimiter is ''.

Solution 2

$str = q(this is a "string");
print $str;

if you mean quotes and apostrophes with 'special characters'

Solution 3

You can use the __DATA__ directive which will treat all of the following lines as a file that can be accessed from the DATA handle:

while (<DATA>) {
   print # or do something else with the lines
}

__DATA__
#!/usr/bin/perl -w
use Some::Module;
....

or you can use a heredoc:

my $string = <<'END';  #single quotes prevent any interpolation
#!/usr/bin/perl -b
use Some::Module;
....
END

Solution 4

The printing is not doing special things to the escapes, double quoted strings are doing it. You may want to try single quoted strings:

print 'this is \n', "\n";

In a single quoted string the only characters that must be escaped are single quotes and a backslash that occurs immediately before the end of the string (i.e. 'foo\\').

It is important to note that interpolation does not work with single quoted strings, so

print 'foo is $foo', "\n";

Will not print the contents of $foo.

Solution 5

You can pretty much use any character you want with q or qq. For example:

#!/usr/bin/perl

use utf8;
use strict; use warnings;

print q∞This is a test∞;
print qq☼\nThis is another test\n☼;
print q»But, what is the point?»;
print qq\nYou are just making life hard on yourself!\n;
print qq¿That last one is tricky\n¿;

You cannot use qq DELIMITER foo DELIMITER. However, you could use heredocs for a similar effect:

print <<DELIMITER
...
DELIMETER
;

or

print <<'DELIMETER'
...
DELIMETER
;

but your source code would be really ugly.

Share:
18,358
Jean
Author by

Jean

Full time Plumber. I speak Malayalam. ആന പോകുന്ന പൂമരത്തിന്‍റെ ചോടെപോകുന്നതാരെടാ.. ആരാനുമല്ല കൂരാനുമല്ല കുഞ്ഞുണ്ണിമാഷും കുട്ട്യോളും - കുഞ്ഞുണ്ണിമാഷ് 7 out of 10 internet users don't know that Ad free browsing is possible https://adblockplus.org/

Updated on June 05, 2022

Comments

  • Jean
    Jean almost 2 years

    Is there an easy way, using a subroutine maybe, to print a string in Perl without escaping every special character?

    This is what I want to do:

    print   DELIMITER <I don't care what is here> DELIMITER
    

    So obviously it will great if I can put a string as a delimiter instead of special characters.