in reply to Variable behaving as a function call

c:\@Work\Perl\monks>perl -wMstrict -le "sub hiya { print 'hello there ', $_[0]; } hiya('sailor'); ;; my $coderef = *hiya{CODE}; $coderef->('everybody'); " hello there sailor hello there everybody

Update: Please see  -> operator discussion The Arrow Operator in perlop;  *foo{THING} discussion in Typeglobs and Filehandles in perldata.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Variable behaving as a function call
by ikegami (Patriarch) on May 11, 2016 at 19:31 UTC
    my $coderef = \&hiya; is far more conventional, but both are ok for subroutines.

    Neither will work for operators such as gmtime and localtime, though.

    Since 5.16:

    my $time_zone_func = $file =~ /^London/ ? \&CORE::gmtime : \&CORE::localtime; strftime($date_format, $time_zone_func->(time))

    Backwards compatible:

    my $time_zone_func = $file =~ /^London/ ? sub { gmtime($_[0]) } : sub { localtime($_[0]) }; strftime($date_format, $time_zone_func->(time))
    or
    my $time_zone_func = sub { $file =~ /^London/ ? gmtime($_[0]) : localtime($_[0]) }; strftime($date_format, $time_zone_func->(time))
      ... \&hiya; is far more conventional ... Neither will work for operators such as gmtime ...

      Oops... Should've remembered that one!


      Give a man a fish:  <%-{-{-{-<

Re^2: Variable behaving as a function call
by dirtdog (Monk) on May 11, 2016 at 19:23 UTC

    works like a charm. Thanks!