Broad Network


Perl Declarations

Perl Syntax – Part 1

Perl Course

Foreword: In this part of the series I talk about Perl declarations.

By: Chrysanthus Date Published: 13 Jun 2015

Introduction

This is part 1 of my series, Perl Syntax. In this part of the series I talk about Perl declarations. Perl Essentials is my term (vocabulary) for topics you have to learn first in Perl. You will have to then use the knowledge of theses topics to learn other topics in Perl. Perl Essentials consists of the topics: Perl Basics, Perl Data Types, and Perl Syntax. After learning these topics, it becomes easy to learn the other topics in Perl, because those other topics use these are bases, to move forward.

Perl’s programming philosophy is different from the philosophy of other languages. So getting a good foundation (the essentials) means you will succeed in understanding the whole of Perl. Perl syntax is the last topic of the three topics in Perl Essentials.

Pre-Knowledge
Before studying this series, you should have read the series, Perl Basics and Perl Syntax, in that order. The links to these series are given below, at the bottom of this page. After this series, your road to become a Perl professional, should be smooth.

Declaring Variables
Declaration is an indication that an entity would exist, while definition means assigning a memory potion.

Scalar Declaration
The following are examples of scalar declaration and assignment.

    my $var;

    $var = "I love you";

The first statement is the declaration and the second is a definition.
Well, Perl is easy going; you do not have to declare a variable before you use it. So, without the first statement, you can have in your program only,

    $var = "I love you";

If you code Perl in strict mode (use strict;), which you should, then only the following without the first statement, is OK:

    my $var = "I love you";

Declaration without assignment, means the undef value has been assigned to the variable; this is done by Perl, unknown to you.

Well, you do not have to declare a variable before you use it, but declaration can help during program maintenance.

Array Declaration
An array is an entity having scalars. The following are examples of array declaration and assignment.

    my @var;

    @var = (10, 'bbb', 30);

The first statement is the declaration and the second is a definition.
As Perl is easy going; you do not have to declare a variable before you use it. So, without the first statement, you can have in your program only,

    @var = (10, 'bbb', 30);

If you code Perl in strict mode (use strict;), which you should, then only the following without the first statement, is OK:

    my @var = (10, 'bbb', 30);

Declaration without assignment, means the undef value has been assigned to the variable; this is done by Perl, unknown to you.

You do not have to declare a variable before you use it, but declaration can help during program maintenance.

Hash Declaration
A hash is an entity having scalar pairs. The following are examples of hash declaration and assignment.

    my %var;

    %var = (AA, 'aaa', BB, 'bbb');

The first statement is the declaration and the second is a definition.
As Perl is easy going; you do not have to declare a variable before you use it. So, without the first statement, you can have in your program only,

    %var = (AA => 'aaa', BB => 'bbb');

If you code Perl in strict mode, which you should, then only the following without the first statement, is OK:

    my %var = (AA => 'aaa', BB => 'bbb');

Declaration without assignment, means the undef value has been assigned to the variable; this is done by Perl, unknown to you.

You do not have to declare a variable before you use it, but declaration can help during program maintenance.

Function Declaration
In Perl, a function is also called a subroutine. The following are examples of function declaration and definition:

    sub fn;

    sub fn
        {
            print "seen";
        }

The first statement is the declaration and then you have a function definition.
As Perl is easy going; you do not have to declare a variable before you use it. So, without the first statement, you can have in your program only,

    sub fn
        {
            print "seen";
        }

Declaration without assignment, means the undef value has been assigned to the variable; this is done by Perl, unknown to you.

You do not have to declare a variable before you use it, but declaration can help during program maintenance.

Filehandle Declaration
A filehandle is normally coded as argument to the Perl built-in open function, without any pre-declaration. The filehandle is a variable that is not preceded by $ or @ or % or &. The following are examples of filehandle declaration and assignment in the non-strict mode:

    fHdle;
    
    open fHdle, "< temp.txt";

The first statement is the declaration and then you have a filehandle application.
As Perl is easy going; you do not have to declare a variable before you use it. So, without the first statement, you can have in your program only,

    open fHdle, "< temp.txt";

Declaration without assignment, means the undef value has been assigned to the variable; this is done by Perl, unknown to you.

You do not have to declare a variable before you use it, but declaration can help during program maintenance.

Label Declaration
You can identify a point (or line) in a program code using a variable type called, label. A label is a word followed by a colon. It is not preceded by $ or @ or % or &. It is not a bareword, because it is followed by a colon. Consider the following program:

use strict;

    my $counter = 0;

    Again:
        print "seen\n";

    if ($counter == 2)
        {
            goto Stop;
        }

    $counter=$counter + 1;
    goto Again;

    Stop:

If you try this program, the output will be:

seen
seen
seen

As the program runs through, the first time from the top, it meets the label, “Again:” and then it prints “seen”. The execution continues downward; it then meets the if-construct; the condition is false and the if-block is skipped. Then the counter is incremented. Then it meets the statement, “goto Again;”. goto is a command, that tells the execution to go to the label and start re-executing from there and coming down.

For the program, the looping execution continues until the if-condition is satisfied, executing the if-block. The block has only one statement that sends execution to the other label, “Stop:” at the bottom of the script. From there, the execution goes downward and ends.

That is one way of using the label variable. The label does not only have to be used with the goto command. The label can be used to identify a loop such as the for-loop (see later).  

The label does not really take an assignment or argument. So it can only be used as a declaration.

We are at the end of the tutorial. Note that the only entity you need to declare in Perl is format – see later. However, under certain situations you have to declare a function before you use it. Apart from formats and functions, you can code an entity without declaration (sometimes, not even functions). However, declaring an entity before definition helps in maintaining the program later.

That is it. See you in the next part of the series.

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

NEXT

Comments

Become the Writer's Fan
Send the Writer a Message