wantarray


When called within a function it will return undef, true or false depending on how was the function called.


undef  if it was called in a void context like f();
true   if it was called in a list context like @x = f(); or print f();
false  if it was called in scalar context like $x = f(); or if($f()) {...}


examples/advanced-perl/wantarray.pl
#!/usr/bin/perl
use strict;
use warnings;

sub f {
    if (not defined wantarray()) {
        print "called in VOID context\n";
    } elsif (wantarray()) {
        print "called in LIST context\n";
    } else {
        print "called in SCALAR context\n";
    }
}

f();          # called in VOID context
my @y = f();  # called in LIST context
my $x = f();  # called in SCALAR context