Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

perl null padding

by m.y (Novice)
on Apr 11, 2008 at 23:09 UTC ( #679883=perlquestion: print w/replies, xml ) Need Help??

m.y has asked for the wisdom of the Perl Monks concerning the following question:

How can I null pad a string without using pack? I need to null pad a string so that its 24 characters. I have this currently:
my $str = "abcde"; $str .= "\0" x (24 - length($str));
The problem is when I do this in a mod_perl environment it never reaches the line after the null padding. Is this not the way to do it?

Replies are listed 'Best First'.
Re: perl null padding
by mr_mischief (Monsignor) on Apr 11, 2008 at 23:30 UTC
    When I do this, I get s length of 24 for $atr:
    $str = 'abcde'; $str .= "\0" x (24 - length $str); print length $str; print "\n";
    How are you using the string? Printing something with null bytes might not do what you expect and probably won't do what you want.
      I get the right length also in a non mod_perl environment. For some reason in mod_perl it never goes past the null padding line.
        For some reason in mod_perl it never goes past the null padding line.

        What direct observation are you making that lead you to conclude that?

        If you comment out the padding line does it correctly print the length of the unpadded string?

        Have you put a die() or similar after the padding to see if execution is really reaching that point?

        It seems much more likely that something you are doing later in the script with the null bytes is causing a mode of failure that prevents the data that's been written to response from appearing on the client.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://679883]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2021-05-12 22:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (134 votes). Check out past polls.

    Notices?