Programming Scripts Articles - Page 16 of 37

Perl Variable Context

Mohd Mohtashim
Updated on 28-Nov-2019 08:11:25

571 Views

Perl treats the same variable differently based on Context, i.e., the situation where a variable is being used. Let's check the following example −Example Live Demo#!/usr/bin/perl @names = ('John Paul', 'Lisa', 'Kumar'); @copy = @names; $size = @names; print "Given names are : @copy"; print "Number of names are : $size";OutputThis will produce the following result −Given names are : John Paul Lisa Kumar Number of names are : 3Here @names is an array, which has been used in two different contexts. First, we copied it into another array, i.e., list, so it returned all the elements assuming that context ... Read More

Perl Hash Variables

Mohd Mohtashim
Updated on 28-Nov-2019 08:08:19

359 Views

A hash is a set of key/value pairs. Hash variables are preceded by a percent (%) sign. To refer to a single element of a hash, you will use the hash variable name followed by the "key" associated with the value in curly brackets.Here is a simple example of using hash variables −Example Live Demo#!/usr/bin/perl %data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40); print "\$data{'John Paul'} = $data{'John Paul'}"; print "\$data{'Lisa'} = $data{'Lisa'}"; print "\$data{'Kumar'} = $data{'Kumar'}";OutputThis will produce the following result −$data{'John Paul'} = 45 $data{'Lisa'} = 30 $data{'Kumar'} = 40

Perl Array Variables

Mohd Mohtashim
Updated on 28-Nov-2019 08:05:52

295 Views

An array is a variable that stores an ordered list of scalar values. Array variables are preceded by an "at" (@) sign. To refer to a single element of an array, you will use the dollar sign ($) with the variable name followed by the index of the element in square brackets.Here is a simple example of using array variables −Example Live Demo#!/usr/bin/perl @ages = (25, 30, 40); @names = ("John Paul", "Lisa", "Kumar"); print "\$ages[0] = $ages[0]"; print "\$ages[1] = $ages[1]"; print "\$ages[2] = $ages[2]"; print "\$names[0] = $names[0]"; print "\$names[1] = $names[1]"; print "\$names[2] = $names[2]";Here we used escape ... Read More

Perl Scalar Variables

Mohd Mohtashim
Updated on 28-Nov-2019 08:03:30

259 Views

A scalar is a single unit of data. That data might be an integer number, floating-point, a character, a string, a paragraph, or an entire web page. Simply saying it could be anything, but only a single thing.Here is a simple example of using scalar variables −Example Live Demo#!/usr/bin/perl $age = 25;                   # An integer assignment $name = "John Paul";         # A string $salary = 1445.50;           # A floating point print "Age = $age"; print "Name = $name"; print "Salary = $salary";OutputThis will produce the following result −Age = 25 Name = John Paul Salary = 1445.5

Mobile

Creating Variables in Perl

Mohd Mohtashim
Updated on 28-Nov-2019 08:01:10

231 Views

Perl variables do not have to be explicitly declared to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.Keep a note that this is mandatory to declare a variable before we use it if we use strict statement in our program.The operand to the left of the = operator is the name of the variable, and the operand to the right of the = operator is the value stored in the variable. For example −$age = 25;             ... Read More

What are Perl String Literals?

Mohd Mohtashim
Updated on 28-Nov-2019 07:58:55

952 Views

Strings are sequences of characters. They are usually alphanumeric values delimited by either single (') or double (") quotes. They work much like UNIX shell quotes where you can use single-quoted strings and double-quoted strings.Double-quoted string literals allow variable interpolation, and single-quoted strings are not. There are certain characters when they are proceeded by a backslash, have special meaning and they are used to represent like newline () or tab (\t).You can embed newlines or any of the following Escape sequences directly in your double-quoted strings −Escape sequenceMeaning\Backslash\'Single quote\"Double quote\aAlert or bell\bBackspace\fForm feedNewline\rCarriage return\tHorizontal tab\vVertical tab\0nnCreates Octal formatted numbers\xnnCreates Hexideciamal ... Read More

What are Perl Numerical Literals?

Mohd Mohtashim
Updated on 28-Nov-2019 07:55:04

414 Views

Perl stores all the numbers internally as either signed integers or double-precision floating-point values. Numeric literals are specified in any of the following floating-point or integer formats −TypeValueInteger1234Negative integer-100Floating point2000Scientific notation16.12E14Hexadecimal0xffffOctal0577

What are different Perl Data Types?

Mohd Mohtashim
Updated on 28-Nov-2019 07:51:52

356 Views

Perl is a loosely typed language and there is no need to specify a type for your data while using it in your program. The Perl interpreter will choose the type based on the context of the data itself.Perl has three basic data types: scalars, arrays of scalars, and hashes of scalars, also known as associative arrays. Here is a little detail about these data types.Sr.No.Types & Description1ScalarScalars are simple variables. They are preceded by a dollar sign ($). A scalar is either a number, a string, or a reference. A reference is actually an address of a variable, which ... Read More

What is Perl Identifiers?

Mohd Mohtashim
Updated on 28-Nov-2019 07:49:03

722 Views

Perl borrows syntax and concepts from many languages: awk, sed, C, Bourne Shell, Smalltalk, Lisp, and even English. However, there are some definite differences between the languages. This chapter is designed to quickly get you up to speed on the syntax that is expected in Perl.A Perl program consists of a sequence of declarations and statements, which run from the top to the bottom. Loops, subroutines, and other control structures allow you to jump around within the code. Every simple statement must end with a semicolon (;).A Perl identifier is a name used to identify a variable, function, class, module, ... Read More

Escaping Characters in Perl

Mohd Mohtashim
Updated on 28-Nov-2019 07:47:17

661 Views

Perl uses the backslash (\) character to escape any type of character that might interfere with our code. Let's take one example where we want to print double quote and $ sign −Example Live Demo#!/usr/bin/perl $result = "This is \"number\""; print "$result"; print "\$result";OutputThis will produce the following result −This is "number" $result

Advertisements