Broad Network


Symbol and Named Unary Operators

Perl Operators – Part 5

Perl Course

Foreword: In this part of the series, I talk about Perl unary operators; a unary operator is an operator that takes one argument.

By: Chrysanthus Date Published: 12 Oct 2015

Introduction

This is part 5 of my series, Perl Operators. In this part of the series, I talk about Perl unary operators; a unary operator is an operator that takes one argument. A binary operator is an operator that takes two arguments (one on its left and the other on its right). The not (!) and negation (-) operators you saw in the previous parts of the series are examples of unary operators. You should have read the previous parts of the series before reaching here, as this is a continuation.

Symbol Unary Operators

The \ Operator
The \ operator is the reference operator. It creates a reference to whatever follows it. The reference is a memory address. The returned reference (memory) address is normally held in a scalar variable. Consider the following code segment:

    my $var1 = "I am a big man.";
    my $sref = \$var1;

    print $$sref;

The \ returns the reference (address) of the scalar, $var. This reference is held in the scalar variable, $sref. You can consider $sref as a reference (it is actually holding an address). You dereference (obtain the value of) a scalar reference using double $ in front of the reference text name (or you can do ${$sref}). You normally do not need to know the exact numerical address of the reference.

Consider the following code segment:

    my @arr = ("one", "two", 3, 4);
    my $aref = \@arr;

    print @{$aref};

The \ returns the reference (address) of the array (variable), @arr. This reference is held in the scalar variable, $aref. You can consider $aref as a reference (it is actually holding an address). You dereference (obtain the value of) an array reference using {..} as illustrated above.

Consider the following code segment:

    my %ha = (Apple => "purple", Banana => "yellow", Pear => "green", Lemon => "green");
    my $href = \%ha;

    print %{$href};

The \ returns the reference of the hash, %ha. This reference is held in the scalar variable, $href. You can consider $href as a reference (it is actually holding an address). You dereference a hash reference using {..} as illustrated above (or you can do %$href).

The ! Operator
This is the NOT operator. The if-block will be executed in the following code:

use strict;

    if (!0)
        {
            print "I am tall";
        }

The if-block is executed, if the condition is true. !(false) gives true.

A practical example for the above code is:

use strict;

    #Let tall mean 20 and short mean 10
    my $me = 20;

    if (!($me == 10))
        {
            print "I am tall";
        }

The - Operator
The negation operator is the minus sign, it changes a positive number to a negative number. Consider the following statement:

    my $var = -5;

5 is by definition a positive number. The negation operator on the right argument of the ordinary assignment operator, turns the 5 to a negative number. The negative number is stored in $var. You can also use – on a variable as in:

    my $va = 6;
    print -$va;

The output will be:

    -6

The + Operator
Any number not proceeded by – is a positive number. You can however use + to emphasize that the number is positive (greater than zero). The following two statements are the same:

    my $va = +8;

    my $va = 8;

Named Unary Operators
The above operators are called symbol operators because they are symbols: \, ! - + and ~. Named unary operators are some predefined functions (with names) that would take only one argument. chdir and rand are examples of named unary operators.

The chdir Operator
This is a named unary operator. This operator us used like a function. Its argument is the full directory path in quotes or the relative path still in quotes. It changes the working directory from the current working directory to the one that is the value of the argument. The working directory is a directory where the Perl script reads or saves files by default. It has the Perl script. The operator can be used like this:

    chdir("c:/dir1/dir2");

or like this:

    chdir("dirC/dirD");

The first case has a full path. The second case has a relative path. For the second case, dirD is a subdirectory to dirC, which is a subdirectory to the working directory (the directory that has the Perl script). Note that even if your operating system is Windows, forward slashes and not back slashes are used in the argument value.

The operator returns true (1) if it succeeds in changing the directory or false (0) if it fails. It can return false if the new directory does not exist.

The rand Operator
This is another unary operator, meaning it takes only one argument. The argument is a positive number greater than 0. The operator returns a random number (mixed fraction) between 0 and the argument value. The statement,

    print rand(10);

can return something like, 6.84417724609375.

Associativity
Symbol unary operators are right associative. This means that if you use a sequence of these operators, the one of the right will be evaluated first, followed by the next one coming to the left, and so on.

Named unary operators are non-associative. This means that for a sequence of such operators some other factor has to be taken into consideration to determine where operation will start.

The Auto-increment Operator
The Increment Operator is, ++. The argument has to be a number. When it is placed in front (prefix) of the argument, it behaves in one way. When it is placed after (postfix) the argument it behaves in another way.

Prefix: When it is prefix, it adds 1 to the argument and returns the incremented argument value. Read and try the following code:

use strict;

    my $id1 = 10;
    my $id2 = ++$id1;
        
    print $id2;

In the code, initially, 10 is assigned to the variable, $id1. Then we have a new statement. In the statement you have a new variable, id2, the assignment operator and then “++$id1”. What interest us here is “++$id1”, where the increment operator is in front of the variable. The value the increment operator returns is assigned to the variable, $id2. If you have tried the code, you would have noticed that the value of $id2 is 11. This means, if used prefix, it increments the argument and then returns the incremented value of the argument.

Postfix: When it is postfix, it returns the argument value before adding 1 to it. The returned value is the original value of the argument. The increased value is the new value of the argument, which is not returned. Read and try the following code.

use strict;

    my $id1 = 10;
    my $id2 = $id1++;
        
    print $id2, "<br>";
    print $id1;

If you have tried the above code, you would have noticed that the value for $id2 is 10 and the final value for $id1 is 11, confirming that the incrementing took place after the value was returned. So, when it is placed postfix, the value of the argument is returned before it is incremented.

The (auto-) increment operator is a unary operator in the sense that it takes one argument, however it also has prefix and postfix features.

The Auto-decrement Operator
The auto-decrement operator is, --. It behaves in the same way as ++ but it subtracts 1 instead of adding 1.

Auto-increment and auto-decrement operators are non-associative operators.

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