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 as below, or enter http://melksh.am/nnnn for individual articles
P256 - Perl 6 Look Ahead [89] When will Perl 6 be available - (2004-10-15)
[113] A Parallel for Perl 6 - (2004-11-09)
[550] 2006 - Making business a pleasure - (2006-01-01)
[582] DWIM and AWWO - (2006-01-30)
[995] Ruby's case - no break - (2006-12-17)
[1215] An update on Perl - where is it going? - (2007-06-03)
[1417] What software version do we teach? - (2007-10-31)
[1721] Perl 6 - When will we have a production release? - (2008-07-26)
[2559] Moving the product forward - ours, and MySQL, Perl, PHP and Python too - (2010-01-01)
[2815] switch and case, or given and when in Perl - (2010-06-17)
[2816] Intelligent Matching in Perl - (2010-06-18)
[2967] Multiway branches in Perl - the given and when syntax - (2010-09-22)
[3077] Perl 6 - significantly nearer, and Rakudo looks very good - (2010-12-02)
P206 - Perl - More Loops and Conditionals [138] Perl - redo and last without a loop - (2004-12-02)
[299] What - no switch or case statement? - (2005-05-03)
[657] The ternary operator in Python - (2006-03-25)
[962] Breaking a loop - Ruby and other languages - (2006-12-03)
[1191] Smart English Output - via PHP and Perl ? : operator - (2007-05-18)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
[1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
[1825] Question Mark - Colon operator (Perl and PHP) - (2008-10-08)
[2711] For loop - checked once, or evety time? Ruby v Perl comparison and contrast - (2010-04-07)
[2824] A pint of Black Rat, and a lazy barman - (2010-06-25)
[2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
[2892] Alternative loops and conditionals in Ruby and Perl - (2010-07-28)
[2972] Some more advanced Perl examples from a recent course - (2010-09-27)
[3200] How a for loop works Java, Perl and other languages - (2011-03-12)
[3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
[3619] Ruby v Perl - a comparison example - (2012-02-21)
[3914] While, for, foreach or something else to loop. - (2012-11-06)
Some other Articles
Chancellor George Osborne inspires Perl ProgramNetiquette for forum newcomersSome more pictures ...File open and read in Perl - modernisationSetting a safety net or fallback value in PerlPython - splitting and joining stringsIterating over a Perl list and changing all itemsWhat is Perl?