Clarification on chomp

后端 未结 5 1006
旧时难觅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 <STDIN>, 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 = <STDIN>; # "Me\n" --> Key DNE, $name_number{$input} not defined
    chomp $input;        # "Me"   --> Key exists, $name_number{$input} defined
    
    0 讨论(0)
  • 2021-01-18 06:11

    I think best practice here is to write:

    chomp(my $input = <STDIN>);
    

    Here is quick example how chomp function ($/ meaning is explained there) works removing just one trailing new line (if any):

    chomp (my $input = "Me\n"); # OK
    chomp ($input = "Me"); # OK (nothing done)
    chomp ($input = "Me\n\n"); # $input now is "Me\n";
    chomp ($input); # finally "Me"
    
    print "$input can be reached at $name_number{$input}\n"; 
    

    BTW: That's funny thing is that I am learning Perl too and I reached hashes five minutes ago.

    0 讨论(0)
  • 2021-01-18 06:12

    OK, as of 1), perl doesn't add any \n at input. It is you that hit Enter when finished entering the number. If you don't specify $/, a default of \n will be put (under UNIX, at least).

    As of 2), chomp will be needed whenever input comes from the user, or whenever you want to remove the line ending character (reading from a file, for example).

    Finally, the error you're getting may be from perl not understanding your variable within the double quotes of the last print, because it does have a _ character. Try to write the string as follows:

    print "$input can be reached at ${name_number{$input}}\n";
    

    (note the {} around the last variable).

    0 讨论(0)
  • 2021-01-18 06:17

    <STDIN> reads to the end of the input string, which contains a newline if you press return to enter it, which you probably do.

    chomp removes the newline at the end of a string. $/ is a variable (as you found, defaulting to newline) that you probably don't have to worry about; it just tells perl what the 'input record separator' is, which I'm assuming means it defines how far <FILEHANDLE> reads. You can pretty much forget about it for now, it seems like an advanced topic. Just pretend chomp chomps off a trailing newline. Honestly, I've never even heard of $/ before.

    As for your other question, it is generally cleaner to always chomp variables and add newlines as needed later, because you don't always know if a variable has a newline or not; by always chomping variables you always get the same behavior. There are scenarios where it is unnecessary, but if you're not sure it can't hurt to chomp it.

    Hope this helps!

    0 讨论(0)
  • 2021-01-18 06:24

    <STDIN> is a short-cut notation for readline( *STDIN );. What readline() does is reads the file handle until it encounters the contents of $/ (aka $INPUT_RECORD_SEPARATOR) and returns everything it has read including the contents of $/. What chomp() does is remove the last occurrence contents of $/, if present.

    The contents is often called a newline character but it may be composed of more than one character. On Linux, it contains a LF character but on Windows, it contains CR-LF.

    See:

    perldoc -f readline
    perldoc -f chomp
    perldoc perlvar and search for /\$INPUT_RECORD_SEPARATOR/
    
    0 讨论(0)
提交回复
热议问题