Home Scripting Perl Tutorial Syntax & Variables
Main Menu
System Verilog
Open Vera
Digital Concepts
Verification Basics
    - Perl Tutorial
        -- Perl Definition
        -- Syntax & Variables
        -- strings
        -- Arrays
        -- Associative Arrays
        -- If & While Loop
        -- File Input
        -- Print Output
        -- Strings and Regular Expressions
        -- Subroutines
        -- Running External Programs
        -- References
        -- Terse Perl
    - Bash
    - Makefile Tutorial
    - Perl Examples
    - Bash Examples
Interview Questions
Computer Architechture
C and C++
AsicGuru Blog
Tags Cloud
Usefull Sites
Know Your IP/Location
Local Information India
Buy Car/Inverter Batteries
Real Estate India
Sports Accessories India
Perl:Syntax and Variable
Share This Articale:

Syntax And Variables

The simplest Perl variables are "scalar" variables which hold a single string or number. Scalar variable names begin with a dollar sign ($) such as $sum or $greeting. Scalar and other variables do not need to be pre-declared -- using a variable automatically declares it as a global variable. Variable names and other identifiers are composed of letters, digits, and underscores (_) and are case sensitive.

Comments begin with a "#" and extend to the end of the line. $x = 2; ## scalar var $x set to the number 2
$greeting = "hello"; ## scalar var $greeting set to the string "hello"

A variable that has not been given a value has the special value "undef" which can be detected using the "defined" operator. Undef looks like 0 when used as a number, or the empty string "" when used as a string, although a well written program probably should not depend on undef in that way. When Perl is run with "warnings" enabled (the -w flag), using an undef variable prints a warning.

if (!defined($binky)) {
   print "the variable 'binky' has not been given a value!\n";

What's With This '$' Stuff?

Larry Wall, Perl's creator, has a background in linguistics which explains a few things about Perl. I saw a Larry Wall talk where he gave a sort of explanation for the '$' syntax in Perl: In human languages, it's intuitive for each part of speech to have its own sound pattern. So for example, a baby might learn that English nouns end in "-y" -- "mommy," "daddy," "doggy". (It's natural for a baby to over generalize the "rule" to get made up words like "bikey" and "blanky".) In some small way, Perl tries to capture the different signature-for-different-role pattern in its syntax -- all scalar expressions look alike since they all start with '$'.


This Articles is written/submitted by puneet (Puneet Aggarwal). You can also contribute to Asicguru.com. Click here to start

Prev << Perl Definition

Next >> strings

Sign In
Login with :-
| | |  
  • Bookmark