Monks,

Lately I've been writing a number of scripts that deal with lists of partial filenames. Most of these are one-offs, so it's natural to write them in the same file as my data and use the DATA filehandle:

#! /usr/bin/perl -w while(<DATA>) { my $path = `find /partial/prefix/path -name "*$_*"`; ... } __END__ filepart1 filepart2 ... filepartN

This is a misguided path. The data after __END__ needs processing ("chewing"):

while(<DATA>) { chomp; ... }

I find this error depressingly easy to make. Save yourself some time, effort, and angst: don't forget to chomp your DATA before you process it.

--
The hell with paco, vote for Erudil!
:wq

Replies are listed 'Best First'.
Re: Chew before you swallow
by vladb (Vicar) on Jun 28, 2002 at 03:31 UTC
    I had a similar problem when I just started out with Perl. However, with time it became a habit of mine to chomp input prior to its consumption ;). There's certainly a lot of other Perl 'traps' to worry about besides this one. What immediately springs to my mind is the perltrap page. This is certainly a good read for both novice and intermediate Perl programmers.

    Generally, however, doing things right in Perl becomes almost a second nature with time. Some basic examples are using strict, doing sanity checks, defensive programming to name a few. The more you develop Perl and concentrate on the quality of your end product/script, the more 'intuitive' these tasks become.

    _____________________
    # Under Construction
Re: Chew before you swallow
by Sweeper (Pilgrim) on Jun 28, 2002 at 05:39 UTC
    From what I have understood, with Perl 6, maybe even Perl 5.8.0, you can open a file and specify at the same time the input will be automatically chomped. Great!

    By the way, I have done this mistake myself. It was a long time ago, I cannot remember specific details.

Re: Chew before you swallow
by stajich (Chaplain) on Jun 28, 2002 at 14:12 UTC
    Of course then I wrote a parser that tests if we are at a line like this:

    '   10  20  30 '

    with this code: /^\s+(\d+)\s+/ and then I hit the one boundary case

    '  10'

    which did not have a trailing space any more since I had chomped.

    Test, Test, test...

Re: Chew before you swallow
by tadman (Prior) on Jun 28, 2002 at 22:19 UTC
    I'm not sure why people lean so heavily on something so massively general-purpose like __DATA__ when there are better ways to do it. Perl will happily parse your stuff for you at compile time:
    my @paths = qw[ filepart1 filepart2 ... filepartN ]; foreach (@paths) { my $path = `find /partial/prefix/path -name "*$_*"`; # ... }
    No chomp, no mess, and no silly errors.

    If this list is just too huge to be practical, you can always toss it into a subroutine, which will allow you to reorder things:
    foreach (paths()) { my $path = `find /partial/prefix/path -name "*$_*"`; # ... } sub paths { qw[ filepart1 filepart2 ... filepartN ]; }
Re: Chew before you swallow
by BUU (Prior) on Jun 28, 2002 at 05:02 UTC
    /me bows down on his knees and sacrifices a small stuffed animal on his st.larry shrine and prays for autochomping file handles.
      Sub-class IO::File and you've already got it. Should be a no-brainer.
      #!/usr/bin/perl -w package IO::File::AutoChomp; use base 'IO::File'; sub getline { my ($self) = @_; # Awkward, I know. return (map { chomp; $_ } $self->SUPER::getline())[0]; } sub getlines { my ($self) = @_; return map { chomp; $_ } $self->SUPER::getlines(); } package main; my $fh = IO::File::AutoChomp->new_from_fd(*DATA, "r") || die "Could no +t open DATA\n"; print join(",", $fh->getlines()); $fh->close(); __END__ The wily rat ate the last donut.