http://www.perlmonks.org?node_id=65344

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

Fellow monks,
I am using Text::Bastardize to output user input in pig latin. But, I am encountering a problem
When I assign `<>' to a scalar, and assign the scalar to `$_', it works fine. but when I `chomp' <>, it will not work.
Sample code:

#!/usr/bin/perl -w use strict; use Text::Bastardize; my $line = <>; $_ = $line; my $latin = Text::Bastardize->new(); $latin->charge($_); print $latin->pig(),"\n"

That code works; but outputs with an appended newline.

When I use:

#!/usr/bin/perl -w use strict; use Text::Bastardize; my $line = <>; $_ = chomp($line); my $latin = Text::Bastardize->new(); $latin->charge($_); print $latin->pig(),"\n"

It doesn't output anything. I do not know whether I am making a basic error, or if Text::Bastardize's `charge' doesn't like the use of chomp for some reason

Please help :)

Replies are listed 'Best First'.
Re: help with (what I consider) a strange problem
by sachmet (Scribe) on Mar 19, 2001 at 07:22 UTC
    perldoc -f chomp should tell you that chomp returns the total number of characters chomped from its arguments. So in essense, you're trying to piglatinize "1". Try changing $latin->charge($_) to $latin->charge($line).

    As an aside, you shouldn't assign to $_. You can do Bad Things(tm) like that if you're not careful.
Re: help with (what I consider) a strange problem
by danger (Priest) on Mar 19, 2001 at 07:16 UTC

    You are assigning the return value of chomp() to $_ ... and chomp() returns whatever it chomped off the end of the string, not the chomped string.

    updateMy bad ... chomp() returns the number of characters chomped off --- it seems Text::Bastardize has a problem with digits and hangs, at least that's what I get with this example:

    #!/usr/bin/perl -w use strict; use Text::Bastardize; my $latin = Text::Bastardize->new(); $latin->charge('blah 10 '); print $latin->pig(),"\n"

    seems like a bug in Bastardize to me.

      Nope... chomp returns number of chars chopped. So:
      $t = "t\n"; $num = chomp($t); print "$num"
      prints "1", whereas:
      $t = "t"; $num = chomp($t); print "$num"
      prints "", whereas:
      @t = ("t\n","u\n","v","w\n"); $num = chomp(@t); print "$num";

      prints "3".

      (edited: error in my code fixed)
Re: help with (what I consider) a strange problem
by dusk (Friar) on Mar 19, 2001 at 08:06 UTC
    Thank you all for your help. I apologize for neglecting to use perldoc
    My code is now:

    #!/usr/bin/perl -w use strict; use Text::Bastardize; chomp(my $line = <>); my $latin = Text::Bastardize->new(); $latin->charge($line); print $latin->pig(),"\n"

    Which works perfectly :)

    update: removed unecessary `$_' assignment. Thanks danger :)

      I can't figure out why that code even compiles. Assigning to chomp should result in the following error: Can't modify scalar safe chop in scalar assignment... But, this error doesn't occur when assigning to chomp if the argument to chomp is an assignment from a filehandle read. Some sort of obscure parsing bug...

      Here's what your code should look like:

      #!/usr/bin/perl -w use strict; use Text::Bastardize; chomp(my $line = <>); my $latin = Text::Bastardize->new(); $latin->charge($line); print $latin->pig(),"\n";