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

Hi,
I have a problem best illustrated with the following short (and 'orrible) script:

#test.dat has one line: bell:\007 open(IN, "test.dat"); while(<IN>){ /(.*):(.*)/; $test{$1} = $2; } print $test{bell} . "no bell\n" $test{bell} = "\007"; print $test{bell} . "bell\n";

For the life of me, I can't seem to get the value read from the file to do anything but send the string "\007" to STDOUT, although the second attempt works fine. Any ideas?

Tom Melly, tom@tomandlu.co.uk

Replies are listed 'Best First'.
Re: reading octal values from file
by Abigail-II (Bishop) on Feb 27, 2003 at 16:44 UTC
    When you are reading from the file, the value of $test{bell} will be a four character string, consisting of a backslash, two zero's and a seven. "\007" is only going to be interpreted a bell character if it appears like this in the program text.

    You might want to use an eval, or chr substr $2, 1.

    Abigail

      Many thanks.

      I've now ended up with the following code (and I'm just putting an integer in the option file - i.e. nak:6)

      I'm also allowing for a string, such as "OK", but can anyone suggest a more elegant solution than the following?

      if($opt{ack} =~ /^\d+$/){ print $live_socket chr($opt{ack}); } else{ print $live_socket $opt{ack}; }
      Tom Melly, tom@tomandlu.co.uk
        Does this help with any ideas?
        use strict; my %test; $test{bell} = 'this is my \007'; $test{bell} =~ s!\\(0\d\d)!chr($1)!ge; print $test{bell} . "bell\n";

        --

        flounder