Mailing List Archive

Should unpack("x", "") die?
I have tried to used the "x" template for unpack to skip chars when
parsing fixed format strings.

For instance, when parsing lines with this format:

xxx xxxxxxx
xxx xxxxxxx

I have coded this as:

while (<>) {
($first, $second) = unpack("A3 x1 A7", $_);
}

This works fine until we read an empty line. This will cause the
script to terminate with the message "x outside of string at ...".
I.e. I have to code it like this to be safe:

while (<>) {
($first, undef, $second) = unpack("A3 a1 A7", $_);
}

Isn't the least surprising behavoiur that unpack just silently ignore
this error, and prints the message as a warning when -w is used.

--
Gisle Aas
Oslonett AS
Re: Should unpack("x", "") die? [ In reply to ]
> From: Gisle Aas <aas@oslonett.no>
>
> I have tried to used the "x" template for unpack to skip chars when
> parsing fixed format strings.
> [...]
>
> while (<>) {
> ($first, $second) = unpack("A3 x1 A7", $_);
> }
>
> This works fine until we read an empty line. This will cause the
> script to terminate with the message "x outside of string at ...".
> I.e. I have to code it like this to be safe:
>
> while (<>) {
> ($first, undef, $second) = unpack("A3 a1 A7", $_);
> }
>
> Isn't the least surprising behavoiur that unpack just silently ignore
> this error, and prints the message as a warning when -w is used.
>
I agree, 'x' should not be special in this regard. It's more reasonable
for '@' to die.

Perhaps a Z format could be added that means 'check at end of string'
(with a count: 'check end of string is within n chars') and die if not.

> --
> Gisle Aas
> Oslonett AS
>
Tim.