Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for 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
  [4609] Mapping an array / list without a loop - how to do it in Perl 6 - (2016-01-03)
  [3939] Lots of ways of doing the same thing in Perl - list iteration - (2012-12-03)
  [3906] Taking the lead, not the dog, for a walk. - (2012-10-28)
  [3870] Writing more maintainable Perl - naming fields from your data records - (2012-09-25)
  [3669] Stepping through a list (or an array) in reverse order - (2012-03-23)
  [3548] Dark mornings, dog update, and Python and Lua courses before Christmas - (2011-12-10)
  [3400] $ is atomic and % and @ are molecular - Perl - (2011-08-20)
  [2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
  [2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
  [2813] Iterating over a Perl list and changing all items - (2010-06-15)
  [2484] Finding text and what surrounds it - contextual grep - (2009-10-30)
  [2295] The dog is not in trouble - (2009-07-17)
  [2226] Revision / Summary of lists - Perl - (2009-06-10)
  [2067] Perl - lists do so much more than arrays - (2009-03-05)
  [1918] Perl Socket Programming Examples - (2008-12-02)
  [1917] Out of memory during array extend - Perl - (2008-12-02)
  [1828] Perl - map to process every member of a list (array) - (2008-10-09)
  [1703] Perl ... adding to a list - end, middle, start - (2008-07-09)
  [1316] Filtering and altering Perl lists with grep and map - (2007-08-23)
  [1304] Last elements in a Perl or Python list - (2007-08-16)
  [968] Perl - a list or a hash? - (2006-12-06)
  [928] C++ and Perl - why did they do it THAT way? - (2006-11-16)
  [773] Breaking bread - (2006-06-22)
  [762] Huge data files - what happened earlier? - (2006-06-15)
  [622] Queues and barrel rolls in Perl - (2006-02-24)
  [560] The fencepost problem - (2006-01-10)
  [463] Splitting the difference - (2005-10-13)
  [355] Context in Perl - (2005-06-22)
  [240] Conventional restraints removed - (2005-03-09)
  [230] Course sizes - beware of marketing statistics - (2005-02-27)
  [140] Comparison Chart for Perl programmers - list functions - (2004-12-04)
  [28] Perl for breakfast - (2004-08-25)

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., 2019: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 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