A useful built-in function is chop. This function takes a single argument within its parentheses - the name of a scalar variable - and removes the last character from the string value of that variable. For example:
$xray = "hello world"; chop($xray); # $xray is now "hello worl"
Note that the value of the argument is altered here, hence the requirement for a scalar variable, rather than simply a scalar value. It would not make sense, for example, to write chop('suey') to change it to 'sue', because there is no place in which to save the value. Besides, you could have just written 'sue' instead.
The value returned is the discarded character (the letter d in world above). This means that the following code is probably wrong:
$xray= chop($xray); # WRONG: replaces $xray with its last character chop($xray); # RIGHT: as above, removes the last character
If chop is given an empty string, it does nothing, and returns nothing, and doesn't raise an error or even whimper a bit.[1] Most operations in Perl have sensible boundary conditions; in other words, you can use them right up to the edges (and beyond), frequently without complaint. Some have argued that this is one of Perl's fundamental flaws, while others write screaming programs without having to worry about the fringes. You decide which camp you wish to join.
When you chop a string that has already been chopped, another character disappears off into "bit heaven." For example:
$fred = "hello world\n"; chop $fred; # $fred is now "hello world" chop $fred; # oops! $fred is now "hello worl"
If you're not sure whether the variable has a newline on the end, you can use the slightly safer chomp operator, which removes only a newline character, [2] like so:
$fred = "hello world\n"; chomp ($fred); # $fred is now "hello world" chomp ($fred); # aha! no change in $fredNext: <STDIN> as a Scalar Value