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

when i tried to print the input removing the new line character it is showing an error
#!/usr/bin/perl -w print chomp <STDIN>

error:Cant modify <HANDLE> in safe chop near <STDIN>

Replies are listed 'Best First'.
Re: print chomp <STDIN>
by marto (Cardinal) on Jul 24, 2008 at 08:29 UTC
    Hi redkar,

    Did you read the documentation for chomp? chomp returns the number of characters removed. Also, you are missing a semicolon from the end of your second line.

    Try the following short example:
    #!/usr/bin/perl use strict; use warnings; print "Please enter some text: "; my $input = <STDIN>; chomp($input); print "You typed $input\n";
    Read Basic Input and Output from the tutorials section of this site, and also the chomp documentation.

    Hope this helps

    Martin
      While picking nit ... the semicolon would be optional on the second statement if it happens to the last statement.
Re: print chomp <STDIN>
by pjotrik (Friar) on Jul 24, 2008 at 08:36 UTC
    You'll have to reverse your word order :-)
    $_=<>; chomp; print;
      thanks for the help.... ll make sure that ill read the documentation properly before bothering the community
Re: print chomp <STDIN>
by broomduster (Priest) on Jul 24, 2008 at 08:34 UTC
    You can only chomp an lvalue (this is the reason for the error that you get). Read

    perldoc -f chomp

    for an explanation.

    Even so, I suspect that

    print chomp (my $value = <STDIN>);
    will not give you what you expect... Here it prints '1', not the chomped line from STDIN. Why, you ask? Because chomp returns the number of characters removed from the end of its argument(s). Again, see the documentation.
Re: print chomp <STDIN>
by Anonymous Monk on Jul 24, 2008 at 08:30 UTC
    You're not using chomp() right. chomp($string) actually modifies $string itself, and its return value is not what you want to print -- it's 1 if chomp did something, 0 if not. see http://perldoc.perl.org/functions/chomp.html
Re: print chomp <STDIN>
by lorn (Monk) on Jul 24, 2008 at 16:08 UTC

    I think that this error:

    error:Cant modify <HANDLE> in safe chop near <STDIN> 

    its not a chomp error, the problem is that chomp try to modify <STDIN> and you cannot modify the <STDIN> :) like you cannot modify the special variable $1 when you get through of regexp.

    PS: sorry for my bad english :)