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

Another one I'm struggling with - Have been asked to write a program which asks for a hex number to be input and convert it to decimal. I did this -
#!/usr/bin/perl #converter.plx use warnings; use strict; print "Please input a hex no. to convert to dec. :"; my $value = <STDIN>; print hex("$value"), "\n";
It will do the conversion, but I get the error -
Illegal hexadecimal digit ' ' ignored at -path- line 7, <STDIN> line 1.
I don't understand where perl is getting the ' from... cheers

Replies are listed 'Best First'.
Re: Illegal hex digit '
by ikegami (Patriarch) on Dec 23, 2006 at 18:58 UTC

    We just answered this for you.

    $value contain the newline from pressing Enter. Use

    chomp( my $value = <STDIN> );

    to remove any trailing newline.

    Ref: chomp

    By the way, you'll probably never want to do func("$var"). Just use func($var).

Re: Illegal hex digit '
by rhesa (Vicar) on Dec 23, 2006 at 18:34 UTC
    It's displaying the newline at the end of the user input (you hit enter, right?). Use chomp to get rid of it:
    chomp( my $value = <STDIN> );
Re: Illegal hex digit '
by thevoid (Scribe) on Dec 23, 2006 at 19:23 UTC
    ooops, chomp again... thanks both