小骆驼 第四章 子程序

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

&print_hello;

sub print_hello
{
    my $hello = 'hello';

    print "$hello\n";
}

###hello

my $three=3;my $four =4;

my $sum = &add;print "$sum\n";

sub add
{
    $three + $four;

}

##7

my $add_print = &add_print;print "$add_print\n"; 

sub add_print
{
    $three + $four;

    print 'I am not sure this will be returned'."\n";
}

##Useless use of addition (+) in void context at test.pl line 33.
##I am not sure this will be returned
##1

my $big_number = 10;my $small_number = 2;

my $choice = &compare(10,2);print "$choice\n";

sub compare
{
    if($_[0]>$_[1])
    {
        return $_[0];
    }
    else
    {
        return $_[1];
    }
}

##10

sub compare_1
{
    if(@_>2)
    {
        print "warning!"
    }
    else
    {
        my ($n,$m) = @_;
    
        if($n > $m)
        {   
            return $n;
        }
        else
        {   
            return $m;
        }
    }
}

my $choice_1 = compare_1 10,2;print "$choice_1\n";

my $choice_2 = &compare_1(10,2,90);print "$choice_2\n";


##10
##warning!1


my $choice_3 = &compare_2(10,2,90,89,45);print "$choice_3\n";

my $choice_4 = &compare_2();print "$choice_4\n";

sub compare_2
{
    my $max = shift @_;
    
    foreach(@_)
    {
    
        if($max < $_)
        {   
            $max = $_;
        }
    }
    return $max;
}

##Use of uninitialized value $choice_4 in concatenation (.) or string at test.pl line 91.

$a = '12345';print $a."\n";

##12345

my $err = &err(1);print "$err\n";

sub err
{
    my $one = $_[0];
    return;
}

##Use of uninitialized value $err in concatenation (.) or string at test.pl line 115.
##

my $number =&new_sum(10,2);print "$number\n";

my $add_once_number = &new_sum(5,7);print "$add_once_number\n";

sub new_sum
{
    my ($n,$m) = @_;

    return $n + $m;
}

##12
##12

my $number_1 =&new_sum_1(10,2);print "$number_1\n";

my $add_once_number_1 = &new_sum_1(5,7);print "$add_once_number_1\n";

sub new_sum_1
{
    my ($n,$m) = @_;

    return $n + $m;
}

##Useless use of addition (+) in void context at test.pl line 33.
##Global symbol "$n" requires explicit package name (did you forget to declare "my $n"?) at test.pl line 146.
##Global symbol "$m" requires explicit package name (did you forget to declare "my $m"?) at test.pl line 146.
##Global symbol "$n" requires explicit package name (did you forget to declare "my $n"?) at test.pl line 148.
##Global symbol "$m" requires explicit package name (did you forget to declare "my $m"?) at test.pl line 148.
##Execution of test.pl aborted due to compilation errors.

my $number_2 =&new_sum_2(10,2);print "$number_2\n";

my $add_once_number_2= &new_sum_2(5,7);print "$add_once_number_2\n";

sub new_sum_2
{
    my ($n,$m) = @_;
    
    state $sum +=  $n + $m;

    return $sum;
}

##24
原文地址:https://www.cnblogs.com/yuanjingnan/p/11061480.html