Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Setting a safety net or fallback value in Perl

Let's say that you want a Perl variable to contain a result which varies depending in the type and values of data fed in. Easy enough - but you need to think about what value you want it to hold if the inputs don't match any of the acceptable values. And there are then two approaches:

1. You can set a Perl variable to a default value early in the code, then override it if you find a matching type and value entered by the user.

2. You can set the variable if you find matching data, and then conclude your code with a "safety net" assignment to set a fallback value if nothing matched at all:
  $name ||= "[Not identified. Not usual colours]";

If you're using the second method, though, you need to take care if an empty string or zero (a false value) is a valid matched value, and you should really rewrite your code:
  defined ($name) or $name = "[Not identified. Not usual colours]";

From Perl 5.10 onwards, a new operator - // - is provided to provide you with a shorthand for "defined or", and you can now write:
  $name //= "[Not identified. Not usual colours]";

We have provided a complete example - [source code here].

(written 2010-06-19, updated 2010-06-20)

 
Associated topics are indexed under
P256 - Perl 6 Look Ahead
  [3077] Perl 6 - significantly nearer, and Rakudo looks very good - (2010-12-02)
  [2967] Multiway branches in Perl - the given and when syntax - (2010-09-22)
  [2816] Intelligent Matching in Perl - (2010-06-18)
  [2815] switch and case, or given and when in Perl - (2010-06-17)
  [2559] Moving the product forward - ours, and MySQL, Perl, PHP and Python too - (2010-01-01)
  [1721] Perl 6 - When will we have a production release? - (2008-07-26)
  [1417] What software version do we teach? - (2007-10-31)
  [1215] An update on Perl - where is it going? - (2007-06-03)
  [995] Ruby's case - no break - (2006-12-17)
  [582] DWIM and AWWO - (2006-01-30)
  [550] 2006 - Making business a pleasure - (2006-01-01)
  [113] A Parallel for Perl 6 - (2004-11-09)
  [89] When will Perl 6 be available - (2004-10-15)

P206 - Perl - More Loops and Conditionals
  [3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
  [3200] How a for loop works Java, Perl and other languages - (2011-03-12)
  [2972] Some more advanced Perl examples from a recent course - (2010-09-27)
  [2892] Alternative loops and conditionals in Ruby and Perl - (2010-07-28)
  [2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
  [2824] A pint of Black Rat, and a lazy barman - (2010-06-25)
  [2711] For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07)
  [1825] Question Mark - Colon operator (Perl and PHP) - (2008-10-08)
  [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
  [1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
  [1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
  [1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)
  [962] Breaking a loop - Ruby and other languages - (2006-12-03)
  [657] The ternary operator in Python - (2006-03-25)
  [299] What - no switch or case statement? - (2005-05-03)
  [138] Perl - redo and last without a loop - (2004-12-02)


Back to
Intelligent Matching in Perl
Previous and next
or
Horse's mouth home
Forward to
File open and read in Perl - modernisation
Some other Articles
Chancellor George Osborne inspires Perl Program
Netiquette for forum newcomers
Some more pictures ...
File open and read in Perl - modernisation
Setting a safety net or fallback value in Perl
Python - splitting and joining strings
Iterating over a Perl list and changing all items
What is Perl?
3603 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

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

PAGE: http://www.wellho.net/mouth/2817_Set ... -Perl.html • PAGE BUILT: Fri Feb 3 14:16:04 2012 • BUILD SYSTEM: wizard