Broad Network


Perl Quote Like Operators

Perl Operators – Part 9

Perl Course

Foreword: In this part of the series I talk about the basics of Perl quote-like operators.

By: Chrysanthus Date Published: 12 Oct 2015

Introduction

This is part 9 of my series, Perl Operators. In this part of the series I talk about the basics of Perl quote-like operators. In Perl, the single or double quote pair, behaves like an operator. You should have read the previous parts of the series before reaching here, as this is a continuation.

q/STRING/
'STRING'
Either of these two formats result in a single quote string. Try the following code:

use strict;

    my $var1 = q/I love you./;
    my $varA = 'Yes, I know that.';

    print $var1, "\n", $varA;

The output is:

    I love you.
    Yes, I know that.

Within single quotes, a variable is not expanded (not replaced by its value). Within single quotes an escaped sequence does not have its effect; for example, \n will not send the rest of the printout to the next line.

qq/STRING/
"STRING"
Either of these two formats result in a double quote string. Note that the q in the former format is doubled, as opposed to the single q above. Try the following code:

use strict;

    my $var0 = 'love';
    my $var1 = qq/I $var0 you./;
    my $varA = "I $var0 you too.";

    print $var1, "\n", $varA;

The output is:

    I love you.
    I love you too.

Within double quotes, a variable is expanded (replaced by its value). Within double quotes an escaped sequence has its effect; for example, \n will send the rest of the printout to the next line.

qx/STRING/
`STRING`
Either of these is the backtick operator. It is a function. The string content is a system command. If it succeeds, it returns the normal message that the system command would return. If it fails, in scalar context, it returns undef, and in list context it returns an empty list. I use the Windows system, so the following code creates two directories using DOS command:

use strict;

    my $ret1 = qx/mkdir c:\dir1/;
    my $retA = `mkdir c:\dirA`;

    print $ret1, "\n", $retA;

The return variables are empty (the system command returned nothing - void), so nothing is printed. Each quotation here is a function. The content of the quotation is a system command.

qw/STRING/
This quoting operator, has no equivalent in terms of single quote, double quote or backtick. It returns a list, which can be received by an array. It separates a string of words into a list of the words and returns the list. It uses whitespace between the words to separate the words. Try the following code:

use strict;

    my @arr = qw/one two three/;

    print $_, "\n" foreach @arr;

The output is:

    one
    two
    three

tr/SEARCHLIST/REPLACEMENTLIST/cdsr
y/SEARCHLIST/REPLACEMENTLIST/cdsr
These two operators are the same. It is the transliteration operator. I talked about it in one of the previous series.

<<EOF
You might have heard about here-doc. This operator is to do with here-doc. It deserves an independent tutorial or series. So I will talk about it later.

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message