Broad Network


Perl Predefined Miscellaneous Functions

Commonly Used Perl Predefined Functions – Part 3

Perl Course

Foreword: In this part of the series, I talk about Perl Predefined Miscellaneous Functions that are commonly used.

By: Chrysanthus Date Published: 19 Oct 2015

Introduction

This is part 3 of my series, Commonly Used Perl Predefined Functions. In this part of the series, I talk about Perl Predefined Miscellaneous Functions that are commonly used. You should have read the previous parts of the series before reaching here, as this is a continuation.

The defined Function
This function tests whether a variable or function is defined. An entity is defined if it occupies space in memory. A variable declared without assignment is not defined. A variable that has been assigned a value, is defined. A function declared without a function body is not defined. A function that has a function body, is defined. The syntaxes for the defined function are:

    defined EXPR
    defined

It returns a Boolean value indicating whether EXPR has a value other than the undefined value, undef. If EXPR is not present, $_ is checked. When you simply declare a variable or a function, the variable has the undef value. Try the following code:

use strict;

    my $varDecl;
    sub fnDecl;

    print '$varDecl is not defined', "\n" if !defined($varDecl);
    print 'fnDecl is not defined', "\n" if !defined(&fnDecl);

    my $varDef = "Love is good.";
    sub fnDef
        {
            print "I am the body\n";
        }

    print '$varDef is defined', "\n" if defined($varDef);
    print 'fnDef is defined', "\n" if defined(fnDef);

Note that in the function declaration case, & is used in the argument of the defined function.

The undef Function
This function removes the variable or function definition, replacing it with undef. The syntax is:

    undef EXPR

It undefines the value of EXPR. Use only on a scalar value, an array (using @ ), a hash (using % ), a subroutine (using & ), or a typeglob (using * ). Note that this is a unary operator, not a list operator. The following code illustrates its use:

use strict;

    my $var = 5;
    my @arr = ("cow", "donkey");
    my %ha = (banana=>"yellow", lime=>"light green");
    sub fn
        {
            print "I am the body\n";
        }

    undef $var;
    undef @arr;
    undef %ha;
    undef &fn;

    print $var, "\n";
    print @arr, "\n";
    print %ha, "\n";
    fn();

The print statements print undef, which is not seen (not printed). The last statement returns an error message, because the function’s body is undefined.

The scalar Function
The syntax is:

    scalar EXPR

Forces EXPR to be interpreted in scalar context and returns the value of EXPR. If EXPR is a list, the last value is returned. Try the following code:

use strict;

    my $var = 5;
    my @animals = ("cow", "donkey", "dog");

    my $sca = scalar("cow", "donkey", "dog");

    print $sca;

The output is, “dog”;

That is it for this 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

BACK

Comments

Become the Writer's Fan
Send the Writer a Message