Home Scripting Perl Tutorial strings
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 strings
Share This Articale:


Strings constants are enclosed within double quotes (") or in single quotes ('). Strings in double quotes are treated specially -- special directives like \n (newline) and \x20 (hex 20) are expanded.
More importantly, a variable, such as $x, inside a double quoted string is evaluated at run-time and the result is pasted into the string. This evaluation of variables into strings is called "interpolation" and it's a great Perl feature. Single quoted (') strings suppress all the special evaluation -- they do not evaluate \n or $x, and they may contain newlines.

$fname = "binky.txt";
$a = "Could not open the file $fname.";                   ## $fname evaluated and pasted in-- neato!

$b = 'Could not open the file $fname.';                   ## single quotes (') do no special evaluation

## $a is now "Could not open the file binky.txt."

## $b is now "Could not open the file $fname."

The characters '$' and '@' are used to trigger interpolation into strings, so those characters need to be escaped with a backslash (\) if you want them in a string. For example:
"nick\@stanford.edu found \$1".

The dot operator (.) concatenates two strings. If Perl has a number or other type when it wants a string, it just silently converts the value to a string and continues. It works the other way too -- a string such as "42" will evaluate to the integer 42 in an integer context.
$num = 42;
$string = "The " . $num . " ultimate" . " answer";
## $string is now "The 42 ultimate answer"

The operators eq (equal) and ne (not equal) compare two strings. Do not use == to compare strings; use == to compare numbers.
$string = "hello";
($string eq ("hell" . "o")) ==> TRUE
($string eq "HELLO") ==> FALSE
$num = 42;
($num-2 == 40)           ==> TRUE
The lc("Hello") operator returns the all lower-case version "hello", and uc("Hello") returns the all upper-case version "HELLO".

Fast And Loose Perl
When Perl sees an expression that doesn't make sense, such as a variable that has not been given a value, it tends to just silently pass over the problem and use some default value such as undef.
This is better than C or C++ which tend to crash when you do something wrong. Still, you need to be careful with Perl code since it's easy for the language to do something you did not have in mind. Just because Perl code compiles, don't assume it's doing what you intended. Anything compiles in Perl.

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

Prev << Syntax & Variables

Next >> Arrays

Sign In
Login with :-
| | |  
  • Bookmark