Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

The Chomp Idea.

by $Variable_B (Initiate)
on Oct 05, 2002 at 01:31 UTC ( [id://202956]=perlquestion: print w/replies, xml ) Need Help??

$Variable_B has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: The Chomp Idea.
by Zaxo (Archbishop) on Oct 05, 2002 at 01:44 UTC

    The chomp function works together with the global variable $/, the input record seperator. If chomp finds the value of $/ at the end of a string, it deletes it from the string. If $/ value is not at the end, there is no action.

    That works nicely with the diamond input operator, <FOO>, since it breaks the input into strings based on the value of $/, leaving that value attached. That makes it common to see:

    while (<FOO>) { chomp; # ...do stuff }

    That idiom also makes use of the pronoun $_, which is set to the line read by 'while diamond..', and which is the default argument for chomp if no other is given.

    After Compline,
    Zaxo

Re: The Chomp Idea.
by Abstraction (Friar) on Oct 05, 2002 at 01:40 UTC
    The short answer: It removes the trailing newline from $scaler.

    The long answer from perldoc.com:

    This safer version of /chop removes any trailing string that corresponds to the current value of $/ (also known as $INPUT_RECORD_SEPARATOR in the English module). It returns the total number of characters removed from all its arguments. It's often used to remove the newline from the end of an input record when you're worried that the final record may be missing its newline. When in paragraph mode ($/ = ""), it removes all trailing newlines from the string. When in slurp mode ($/ = undef) or fixed-length record mode ($/ is a reference to an integer or the like, see perlvar) chomp() won't remove anything. If VARIABLE is omitted, it chomps $_.
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: The Chomp Idea.
by sauoq (Abbot) on Oct 05, 2002 at 01:44 UTC

    You can use chomp as chomp $scalar or chomp @array. If any of its arguments end with $/ (the input record separator; usually "\n") it will chomp off that part. It returns the total number of characters that it removes (from all of its arguments.)

    -sauoq
    "My two cents aren't worth a dime.";
    
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://202956]
Approved by chromatic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-28 14:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found