File::Slurp and using it with chomp

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Hi all.

I've been using File::Slurp to avoid using open, mostly because I
will be using it in the future in scripts where many files get read, that
often consist of only one line. It would be a bit tedious to do it without
File::Slurp, and so I'm learning to use it to practice a bit.
qw( slurp ) makes sure slurp() is an alias for read_file() to shorten it
even further. The problem with the script below is, that often these
single line files contain a newline at the end. I have been unable to use
chomp() in an efficient way to counter this (here's an example):

perl -e 'use File::Slurp ; print chomp(read_file("/proc/uptime")) . "\n";'
Can't modify non-lvalue subroutine call in chomp at -e line 1, near ")) "
Execution of -e aborted due to compilation errors.

It strikes me as inefficient to store everything in a scalar first and
then remove the newlines with a regex. Trying to use map() only got me in
trouble further. Any suggestions?

regards, loccy

(the code below prints newlines, I'd like to have them removed from the
output efficiently)

use File::Slurp qw( slurp );

(glob "/sys/bus/usb/devices/*/*/*/block /sys/bus/usb/devices/*/*/*/*/block" ) {
    $_ = readlink $_;

for ( keys %dev ) {
    print(  "$_ "
          . slurp("/sys/block/$_/device/vendor")
          . slurp("/sys/block/$_/device/model")
          . "\n" );

# output should be something like "sda Generic USB Flash Disk"

Site Timeline