#!/usr/bin/perl -w sub polymorphic { my $context=wantarray(); if ($context) { print "list context\n"; return @_; } elsif (defined($context)) { print "scalar context\n"; return join ":", @_; } else { warn "value of polymorphic() used in void context. Values were (\n"; # so no point in returning anything, but do print print join ",", @_; print ")\n"; } } sub show_args { print "got ", scalar(@_), " args:\n"; map {print " $_\n" } @_; } show_args("a", polymorphic("b","c","d")); my $string=polymorphic(1,2,3); my @array=polymorphic(4,5,6); show_args($string,@array); # make warning 1 polymorphic("unused"); # following is tricky because we don't know context until tricky is # actually called. sub tricky { polymorphic(@_); } my $string2=tricky(qw(a b c d)); my @array2=tricky(qw(s t u v)); show_args($string2,@array2); # make warning 2 tricky(qw(fee fi fo fum)); # what happens if we use return keyword explicitly? sub tricky_2 { return polymorphic(@_); } my $string3=tricky_2(qw(a e i o u)); my @array3=tricky_2(qw(w x y z)); show_args($string3,@array3); # (turns out we can use return or not; same result--no warning) # (but this should generate one) tricky_2(5,6,7,8); sub tricky_3 { my $first=shift; my $second=shift; my @remaining=@_; print "$first, $second, ("; print join ",", @remaining; print ")\n"; };