Search notes:

Perl operator // (slash slash)

The slash slash operator in perl returns the left side value if it is defined, otherwise it returns the right side value.
This differs from the || operator in that the || operator checks for truth rather than definendness.
$x //= 'value' assigns 'value' to $x unless $x is defined.
The slash slash operator comes in handy, for example, when handling null values with one of the DBD modules.
use warnings;
use strict;
use feature 'say';

my $p;
my $q  = "foo";
my $r  = "";
my $t  = "0";


say $p // "bar"; # bar
say $q // "bar"; # foo
say $r // "bar"; #
say $t // "bar"; # 0

say "----------";

$p //= "V";
$q //= "V";
$r //= "V";
$t //= "V";

say $p; # v
say $q; # foo
say $r; #
say $t; # 0
Github repository about-perl, path: /operators/slash_slash.pl

See also

Perl operators

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759414189, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Perl/operators/slash_slash(75): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78