Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Help reg printing non printable hex characters

by grinder (Bishop)
on Jun 18, 2008 at 08:20 UTC ( [id://692658]=note: print w/replies, xml ) Need Help??


in reply to Help reg printing non printable hex characters

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

Replies are listed 'Best First'.
Re^2: Help reg printing non printable hex characters
by almut (Canon) on Jun 18, 2008 at 08:27 UTC
    # 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://692658]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-19 16:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found