in reply to Re: Pack uneven length hex
in thread Pack uneven length hex

Thank you for the reply. I don't want to set maximum length on a hex string. I work with very big numbers so hex() is out of question, and in my case byte string is actually what I want to get.
The map/for is quite interesting, however I was hoping there's a clever way to work with parameters in pack() itself, which would change it's behavior to pad left side (I couldn't find anything like that on my own).
Inserting a tiny function like this seems to be the best solution, however as this is meant to be in a CPAN package I wanted to keep it as clean as possible, and at least two of my files will require this functionality. Why do it even pad numbers on the right side?

Replies are listed 'Best First'.
Re^3: Pack uneven length hex
by syphilis (Archbishop) on Nov 23, 2018 at 10:50 UTC
    I was hoping there's a clever way to work with parameters in pack() itself, which would change it's behavior to pad left side

    Not sure that it can be classified as "clever", but if you "scalar reverse()" twice, it seems to produce the correct result (for your given example, at least) :
    print scalar reverse unpack "H*", pack("H*", scalar reverse "a12");
    Cheers,
    Rob
Re^3: Pack uneven length hex
by Eily (Monsignor) on Nov 23, 2018 at 10:57 UTC

    however I was hoping there's a clever way to work with parameters in pack() itself
    As far as I can tell, there isn't one. Though I agree that would have been interesting.

    this is meant to be in a CPAN package I wanted to keep it as clean as possible
    I suppose that's why you don't want to put the function in its own file? Because it kind of belongs in a separate module?

    Why do it even pad numbers on the right side?
    Because adding things on the right of your data should not change how the left part is interpreted, I guess

    I just thought of another way to do what you want: pack 'H*', 'a12' =~ s/^(.(..)*)$/0$1/rxs. Still not as clear as a function though.