fork download
  1. #!/usr/bin/perl
  2.  
  3. # Idiom #267 Pass string to argument that can be of any type
  4. # https://p...content-available-to-author-only...s.org/idiom/267/pass-string-to-argument-that-can-be-of-any-type
  5.  
  6. use v5.10;
  7.  
  8. use Scalar::Util 'looks_like_number';
  9.  
  10. sub foo {
  11. my ($x) = @_;
  12. return 'Nothing' if ref $x ne '' or looks_like_number($x);
  13. # return 'x is a reference' if ref $x ne '';
  14. # return 'x looks like a number' if looks_like_number($x);
  15. # return 'x is a string';
  16. return $x;
  17. }
  18.  
  19. say foo( [] );
  20. say foo( 42 );
  21. say foo( 'Hello World' );
  22.  
Success #stdin #stdout 0.01s 5620KB
stdin
Standard input is empty
stdout
Nothing
Nothing
Hello World