Clarification on chomp

后端 未结 5 1007
旧时难觅i
旧时难觅i 2021-01-18 05:39

I\'m on break from classes right now and decided to spend my time learning Perl. I\'m working with Beginning Perl (http://www.perl.org/books/beginning-perl/) and I\'m finish

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-18 06:07

    Though it may be obvious, it's still worth mentioning why the chomp is needed here.

    The hash created contains 4 lookup keys: "Me", "Home", "Emergency" and "Lookup"

    When $input is specified from , it'll contain "Me\n", "Me\r\n" or some other line-ending variant depending on what operating system is being used.

    The uninitialized value error comes about because the "Me\n" key does not exist in the hash. And this is why the chomp is needed:

    my $input = ; # "Me\n" --> Key DNE, $name_number{$input} not defined
    chomp $input;        # "Me"   --> Key exists, $name_number{$input} defined
    

提交回复
热议问题