开发者

Problems with separator-encoding

If I run this script as it is, it works.

But why does this not work with cgi?

When I use _\01_ instead of _\00_ it works with cgi too.

#!/usr/bin/env perl
use warnings;
use 5.012;


###    script_1.cgi    #########################################

my @arra开发者_开发技巧y = ( '1524', '2.18 MB', '09/23/03', '_cool_name_', 'type' );
my $row = join "_\00_", @array;
say $row;
# submit $row to script_2.cgi


###    script_2.cgi    #########################################
# ...
# my $row = $cgi->param('row');
# my $name;

if ( $row =~ /_\00_([^\00]+)_\00_type\z/ ) {
#   $name = $1;
    say "Name: <$1>";
} else {
    die "<$row> $!";
}

# Software error:
# <1524_�_2.18 MB_�_09/23/03_�__cool_name__�_type>  at script_2.cgi line of "die "<$row> $!";"


Works for me, says _cool_name_. You're probably running afoul of CGI.pm using \0 already for itself, but since you did not post your complete code, no one can say for sure.

I'll use the opportunity to unask the question. The lessons you should learn are:

  1. Avoid rolling your own serialisation scheme. As a beginner, you have made the typical mistake of not encoding the separator if it occurs in the data (c.f. double backslash in string expressions and double percent in sprintf expressions). The array could have been passed intact unjoined via e.g. JSON.
  2. Instead of two scripts, these should be two subroutines in the same program. This way, you are able to pass data structures without the need to serialise.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜