Use of the => operator

N

niall.macpherson

I needed a bit of code to convert a string to the hex values for the
individual chars and found the following 2 examples


my $teststr = 'abcABC123XYZ';

my $hexstr = join (' ', map (sprintf("%2.2x", ord($_)), split(//,
$teststr))); ## method 1

$hexstr = unpack 'H* ' => $teststr; ## method 2

Both of these work fine , although the second one doesn't separate the
values with spaces (which I needed) so I did

$hexstr =~ s/(\d\d)/$1 /g;

Method 1 was a useful exercise in understanding how map works (which I
previously hadn't been too comfortable with), but I decided to go with
method 2 as it looks a bit cleaner.

I think I understand how unpack works , however I have only previously
seen the => operator used in initialising hashes and I am not clear how
it works in this context. I looked at perldoc perlop but couldn't spot
anything relevant.

I'm therefore unsure how I can insert the spaces in method 2 in the
same line because I don't understand how / why the => operator is used
here.

Can someone please explain ?

Thanks
 
A

Anno Siegel

I needed a bit of code to convert a string to the hex values for the
individual chars and found the following 2 examples


my $teststr = 'abcABC123XYZ';

my $hexstr = join (' ', map (sprintf("%2.2x", ord($_)), split(//,
$teststr))); ## method 1

$hexstr = unpack 'H* ' => $teststr; ## method 2
[...]

I think I understand how unpack works , however I have only previously
seen the => operator used in initialising hashes and I am not clear how
it works in this context. I looked at perldoc perlop but couldn't spot
anything relevant.

Look for "comma operator" in perlop.

As used above, "=>" is entirely equivalent to a comma.

Anno
 
T

Tad McClellan

$hexstr = unpack 'H* ' => $teststr; ## method 2

I have only previously
seen the => operator used in initialising hashes and I am not clear how
it works in this context. I looked at perldoc perlop but couldn't spot
anything relevant.


See the "Comma Operator" section.

I'm therefore unsure how I can insert the spaces in method 2 in the
same line


Use unpack() in a list context instead of in a scalar context,
then join up the list elements the way you want them:

$hexstr = join ' ', unpack('H* ' => $teststr); # untested

because I don't understand how / why the => operator is used
here.

Can someone please explain ?


It is just a comma, the same as if you had:

$hexstr = unpack 'H* ' , $teststr; ## method 2


So, it has no relationship to getting the spaces that you want.
 
D

Dr.Ruud

Tad McClellan schreef:
(e-mail address removed) wrote:
$hexstr = unpack 'H* ' => $teststr; ## method 2
[...]
I don't understand how / why the => operator is used
here.

It is just a comma, the same as if you had:

$hexstr = unpack 'H* ' , $teststr;

See also:

perl -MO=Deparse -e '$hexstr = unpack q{H* } => $teststr'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top