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

On winxp, activestate, I run:
find | perl -ne "print qq($_) . qw(foo)"
Output is:
(snip): foo.\translation.php foo.\update.php foo.\_test_bidmanagement.ppj foo.\_test_bidmanagement.ppx foo
I am sure I am overlooking something completely obvious, but why is "foo" at the beginning of each line instead of at the end?

Replies are listed 'Best First'.
Re: Concatenation connected to find pipe behaves wierdly.
by sfink (Deacon) on Jan 31, 2006 at 18:05 UTC
    find is giving you a list of filenames terminated by newlines. So when you print qq($_), you're printing out a newline after your filename. So all your foo are belong to us... dammit... all of your foo end up just after a newline, aka the beginning of the next line.

    The quick fix isfind | perl -lne "print qq($_) . q(foo)"

    That will strip off the newline when reading each line in, and append a newline to the output of print.

      And while we're there we may also remind him not to unnecessarily quote all variables:

      perl -lne "print $_ . 'foo'"

      Alternatives:

      • interpolation, but not on a single variable: qq(${_}foo) - ugly, but qq($_foo) wouldn't work;
      • introducing -p:
        perl -lpe "$_.='foo'"
      • ...
Re: Concatenation connected to find pipe behaves wierdly.
by chargrill (Parson) on Jan 31, 2006 at 18:29 UTC

    find alone on a unix displays a help/usage message. find alone in a CMD window performs similarly.

    The following on a unix is a bit weird to me at first:

    $ find . -name "*.pl" | perl -ne "print qq($_) . qw(foo)" foofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoof +oofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoof +oofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoof +oofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoof +oofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoof +oofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoofoo

    ... time passes, nodes are read, code is crafted, head is scratched, lightbulb illuminates, reply text is angrily deleted ...

    I just went through the exercise of determining why I easily replicated tphyahoo's behavior, with an incorrect dissection of the differences between -n and -p, then I finally realized the same thing sfink did...

    $ perl -ne 'print "$_ foo" ' perl.txt ./.cpan/build/DateTime-0.30/tools/leap_seconds_header.pl foo./.cpan/build/DateTime-TimeZone-0.40/t/check_datetime_version.pl

    Had you taken the TOP 'snippet' of output, you'd've VERY quickly seen the explanation :)



    --chargrill
    $/ = q#(\w)# ; sub sig { print scalar reverse join ' ', @_ } + sig map { s$\$/\$/$\$2\$1$g && $_ } split( ' ', ",erckha rlPe erthnoa stJu +" );
Re: Concatenation connected to find pipe behaves wierdly.
by graff (Chancellor) on Feb 01, 2006 at 02:50 UTC
    Another way to try would be:
    find . -print0 | perl -0pe "s/\0/foo\n/"
    Check perlrun for the "-0" option -- it's meant to take advantage of the "-print0" option on find, to avoid the possibility of getting screwed because a file name happens to include newline characters.
      Or you could spell that
      find . -print0 | perl -l -0 -ne 'print $_ . "foo"'
      (Quotes this time are for a Unix shell.)

      It's a little wacky due to the order sensitivity of the command line arguments: perl -0 -l -ne ... would not work. But as graff said, see perlrun for the details.