Perl Subroutine Prototyping — The correct way to do it

后端 未结 5 1286
滥情空心
滥情空心 2020-12-10 06:48

I have a subroutine called debug I use in my code. It basically allows me to see what\'s going on, etc.

sub debug {
    my $message      = shift         


        
5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-10 07:07

    If I am understanding correctly, you want to prototype and predeclare so that you can use the function (prototyped and braceless) within the same file. This is what the subs pragma is for.

    For example, this code works correctly:

    #!/usr/bin/env perl
    
    use strict;
    use warnings;
    
    use subs qw/mysay/;
    
    mysay "Yo";
    mysay "Yo", "Joel";
    
    sub mysay ($;$) {
      my $message = shift;
      my $speaker = shift;
      if (defined $speaker) {
        $message = "$speaker says: " . $message;
      }
      print $message, "\n";
    }
    

提交回复
热议问题