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

Hi All, i am trying to write a code which will take a non printable character as an input and used as a separator for different fields ex : if user gives an input \xd1 the o/p should be field1 ¨h field2 initially i have written a code to get the below lines in my program hex.pl
use strict; my $seperatorArg; $seperatorArg = shift(@ARGV); print "field1 $seperatorArg field2";
when i ran  perl hex.pl \xd1 i got the below o/p field1 \xd1 field2 if i hardcode the value in my code like
$seperatorArg = "\xd1";
i am getting the desired. can anybody explain my below understanding is correct? what i am thinking is when i hard code, the value i gave in double quotes and hence the variable interpoplation is occuring which results in hex character
when i gave the same value as input in command line the value is getting in single quotes which is not getting interpolated and hence the same value is being printed.
please point me to the right direction if i am in wrong direction, so tht i can put more effort to make my code to behave in the way i want another way i tried is
use constant HEXD1 => "\xd1"; use constant HEXDC => "\xdc"; $arg = shift(@ARGV); if ($arg eq "HEXD1") { $arg = HEXD1; print " HEX D1 IS $arg \n"; } elsif ($arg eq "HEXDC") { $arg = HEXDC; print " HEX DC IS $arg \n"; }
i am running the program as below
perl hex.pl HEXD1 perl hex.pl HEXDC
i know this is a bad way of coding.... :( trying in different ways.. but couldnt get the expected o/p. Kindly provide me the pointers on this.

Replies are listed 'Best First'.
Re: Help reg printing non printable hex characters
by grinder (Bishop) on Jun 18, 2008 at 08:20 UTC

    If you put \xd1 on the command line, your program will see that literally. In order to match it literally, you have to say

    use constant HEXD1 => "\\xd1";

    Or something like that. You're right that the code is a little strange. I think you'll find it much simpler to say:

    # from the command line perl d1 perl d3 # in the code my $arg = hex(shift @ARGV);

    update: almut is absolutely correct, that should read

    my $arg = chr(hex(shift @ARGV));

    or even

    my $arg = chr hex shift;

    ... but I didn't want to obsfuscate the code unnecessarily (shift of what?)

    • another intruder with the mooring in the heart of the Perl

      # in the code my $arg = hex(shift @ARGV);

      Wouldn't that rather be

      my $arg = chr hex(shift @ARGV);

      (hex gives a number (e.g. 209 for "d1"), but it seems the OP actually wants the respective char, i.e. chr(209)... )

      BTW, another possibility would be

      my $sep = pack "H2", (shift @ARGV); # with input being 'd1', for exam +ple

      Or, if you insist on specifying the \x prefix on input, you could extract the hex part with a regex

      my $sepArg = shift @ARGV; my ($hex) = $sepArg =~ /\\x([\da-fA-F]+)/; my $sep = chr hex $hex; my $str = "field1 $sep field2";

      Update: in theory, you could also do

      my $sepArg = shift @ARGV; # input being '\xd1', for example my $str = eval qq("field1 $sepArg field2");

      but don't do that if your user input comes from untrusted sources... (any code could be executed!)

        Hi All, Thanks a lot for support and guidance. my original requirement is to get the data from an oracle table and prepare an extract with the configured column and line seperators. input is configuration purpose only. anyways i will avoid the spl characters. once again Thanks a lot.