perl メモ(サブルーチン)

#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
{
  # サブルーチン
  sub logger {
    my $args = shift;
    return $args;
  }
  say logger("sub"); # sub

  sub logger2 {
    if (@_ != 2) {
      say "error";
      return;
    }
    my ($args, $args2) = @_;
    say "$args, $args2";
  }
  logger2(0, 1, 2); # error
  logger2("0", 1); # 0, 1

  sub hashref { +{ @_ } }
  my $href = hashref( (abc => "hashref") );
  say $href->{abc}; # hashref

  my $coderef = sub { say "coderef" };
  &$coderef; # coderef

  sub newprint {
    my $x = shift;
    return sub { my $y = shift; return "$x, $y"; };
  }
  my $np = newprint("a");
  say &$np("b"); # a, b

  sub newprint2 {
    return sub { return @_; }
  }
  $np = newprint2();
  say &$np("abc"); # abc

  sub max {
    my $max = shift(@_);
    foreach my $foo (@_) {
      $max = $foo if $max < $foo;
    }
    return $max;
  }
  say max(1, 2, 30, 4, 5); # 30
}
exit;