in reply to Re^2: pack() returns an unusable string
in thread pack() returns an unusable string

And here's a version with Data::Dumper and some config vars that also works.

#!/usr/bin/perl -l use strict; # https://perlmonks.org/?node_id=11133064 use warnings; my $template = 'd<'; my $nv = 2.4; $nv = 4135; # NOTE errors for this number my $p = pack $template, $nv; use Data::Dumper; $Data::Dumper::Terse = $Data::Dumper::Useqq = 1; my $s = Dumper $p; print ">$s<\n"; system $^X, '-wle', "use strict; print unpack('H*', $s);";

Outputs:

>"\0\0\0\0\0'\260\@" < 000000000027b040

note that it adds a newline, but perl doesn't care.

Replies are listed 'Best First'.
Re^4: pack() returns an unusable string
by syphilis (Archbishop) on May 27, 2021 at 02:39 UTC
    And here's a version with Data::Dumper and some config vars that also works

    Neither this nor the rendition using Data::Dump works for me on Windows:
    C:\_32\pscrpt>type test3.pl use strict; # https://perlmonks.org/?node_id=11133064 use warnings; my $template = 'd<'; my $nv = 2.4; $nv = 4135; # NOTE errors for this number my $p = pack $template, $nv; use Data::Dumper; $Data::Dumper::Terse = $Data::Dumper::Useqq = 1; my $s = Dumper $p; print ">$s<\n"; system $^X, '-wle', "use strict; print unpack('H*', $s);"; C:\_32\pscrpt>perl test3.pl >"\0\0\0\0\0'\260\@" < syntax error at -e line 1, at EOF Execution of -e aborted due to compilation errors. C:\_32\pscrpt>
    I guess it's something to do with Windows command line quoting rules.
    Mind you, having it work on Windows specifically is not important.
    Now that I have a working example, I can while away some time porting your fix to Windows when I have nothing better to do.

    Thanks tybalt89 !!

    Cheers,
    Rob

    Thanks

      Just another guess (to tweak GrandFather) because I don't have a Windows system to test on.

      #!/usr/bin/perl -l use strict; # https://perlmonks.org/?node_id=11133064 use warnings; my $template = 'd<'; my $nv = 2.4; $nv = 4135; # NOTE errors for this number my $p = pack $template, $nv; use Data::Dump qw(pp); my $s = (pp $p) =~ s/"(.*)"/qq($1)/sr; # tweak for different delimiter print ">$s<\n"; system $^X, '-wle', "use strict; print unpack('H*', $s);";
        Just another guess ...

        Yes, that works fine on Windows as is:
        C:\_32\pscrpt>perl test4.pl >qq(\0\0\0\0\0'\xB0\@)< 000000000027b040
        But not when I change $template to "D<" and $nv to 2.4:
        C:\_32\pscrpt>perl test4.pl >pack(qq(H*","9a999999999999990040000000000000))< syntax error at -e line 1, at EOF Execution of -e aborted due to compilation errors.
        (I can dig into this later if you're getting tired of it.)

        Cheers,
        Rob