Wrox Programmer Forums
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old May 12th, 2007, 11:50 AM
Authorized User
Join Date: Apr 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chomp

I do not no what chomp does to a vairable and what it does to this simple script.

# convert1.plx
use warnings;
use strict;

my ($value, $from, $to, $rate, %rates);
%rates = (
    pounds => 1,
    dollars => 1.6,
    marks => 3.0,
    "french francs" => 10.0,
    yen => 174.8,
    "swiss francs" => 2.43,
    drachma => 492.3,
    euro => 1.5

print "Enter your starting currency: ";
$from = <STDIN>;
print "Enter your target currency: ";
$to = <STDIN>;
print "Enter your amount: ";
$value = <STDIN>;

$rate = $rates{$to} / $rates{$from};

print "$value $from is ",$value*$rate," $to.\n";

Old May 14th, 2007, 05:54 AM
Friend of Wrox
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts


[chomp] 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 the perlvar manpage) chomp() won't remove anything. If VARIABLE is omitted, it chomps $_. Example:

        while (<>) {
            chomp; # avoid \n on last field
            @array = split(/:/);
            # ...

    If VARIABLE is a hash, it chomps the hash's values, but not its keys.

    You can actually chomp anything that's an lvalue, including an assignment:

        chomp($cwd = `pwd`);
        chomp($answer = <STDIN>);

    If you chomp a list, each element is chomped, and the total number of characters removed is returned.

Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk

Similar Threads
Thread Thread Starter Forum Replies Last Post
ruby on rails>>chomp ckonrad BOOK: Beginning Ruby on Rails 4 March 28th, 2007 02:24 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.