Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Context - List, scalar and double quotes

Context in Perl

In Perl, you can store a number or a string (or a reference or a regular expression) in a scalar variable, and you can store a collection of such scalars in a list.

Scalar variables are written starting with a $, and list variable names starting with an @. You can perform operations on a list as a whole using standard operators - known as "list context". If you do something that really isn't sensible on a list, then Perl will do it in scalar context instead, where it takes the length of the list and uses that.

If you write a list within double quotes, that's in effect a third context - "double quote context" - and it expands the list with a space between each element.

Here's an example:

@sarnie = ("Cheese","Tomato","Marmite","Onion");

@pizza = @sarnie; # list CONTEXT
$lasagne = @sarnie; # scalar CONTEXT - the length of a list
$pie = "@sarnie"; # double quote CONTEXT - string

print @sarnie,"\n";
print @pizza,"\n";
print $lasagne,"\n";
print $#sarnie,"\n"; # Top index number (== length of list -1)
print $pie,"\n";
print "@sarnie\n"; # Double quote context again

($main,$trimming) = @sarnie; # ( and ) force list context
($another) = @sarnie; # list context
$yetanother = @sarnie; # scalar context

print "That's a $main sarnie with a bit of $trimming\n";
print "Looking at $another and $yetanother\n";

Here's what you'll see if you run that:

[localhost:~/pfeb] graham% perl llold
CheeseTomatoMarmiteOnion << List context
CheeseTomatoMarmiteOnion << List context
4 << Scalar Context
3
Cheese Tomato Marmite Onion << Double Quote Context
Cheese Tomato Marmite Onion << Double Quote Context
That's a Cheese sarnie with a bit of Tomato
Looking at Cheese and 4
[localhost:~/pfeb] graham%


See also Perl Lists

Please note that articles in this section of our web site were current and correct to the best of our ability when published, but by the nature of our business may go out of date quite quickly. The quoting of a price, contract term or any other information in this area of our website is NOT an offer to supply now on those terms - please check back via our main web site

Related Material

Perl - Lists
  [28] - ()
  [140] - ()
  [230] - ()
  [240] - ()
  [355] - ()
  [463] - ()
  [560] - ()
  [622] - ()
  [762] - ()
  [773] - ()
  [928] - ()
  [968] - ()
  [1304] - ()
  [1316] - ()
  [1703] - ()
  [1828] - ()
  [1917] - ()
  [1918] - ()
  [2067] - ()
  [2226] - ()
  [2295] - ()
  [2484] - ()
  [2813] - ()
  [2833] - ()
  [2996] - ()
  [3400] - ()
  [3548] - ()
  [3669] - ()
  [3870] - ()
  [3906] - ()
  [3939] - ()
  [4609] - ()

resource index - Perl
Solutions centre home page

You'll find shorter technical items at The Horse's Mouth and delegate's questions answered at the Opentalk forum.

At Well House Consultants, we provide training courses on subjects such as Ruby, Lua, Perl, Python, Linux, C, C++, Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer) many questions, and answers to those which are of general interest are published in this area of our site.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/solutions/perl-con ... uotes.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard