Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks

I copied the sub "dec2bin" from section 2.4 "Converting Between Binary and Decimal" in the Perl Cookbook (page 48 in my copy).

sub dec_to_bin { my $flag = shift ; my $str = unpack("B32", pack("N", $flag)) ; $str =~ s/^0+(?=\d)// ; return $str ; }

When I run it, I get this strange warning ...

Ambiguous call resolved as CORE::unpack(), qualify as such or use & at ./utils.pl line 190.
Ambiguous call resolved as CORE::pack(), qualify as such or use & at ./utils.pl line 190.

The result seems OK to me -- so what is the problem? Anyone here know?

The OS is FreeBSD.

Replies are listed 'Best First'.
Re: "Ambiguous call" in decimal to binary conversion
by bart (Canon) on Apr 29, 2004 at 10:21 UTC
    You must have some pack and unpack subs defined in the current package... Yes it works as it should, as CORE::pack() and CORE::unpack() are used, just like the warnings say.

    So what can you do? Like the warning says, use CORE::pack(...) or &pack(...) in your call, instead of plain pack(...), making clear which one you want, or just don't use "pack" as your sub name. Ditto for "unpack".

Re: "Ambiguous call" in decimal to binary conversion
by theorbtwo (Prior) on Apr 29, 2004 at 10:22 UTC

    You have subroutines named "unpack" and "pack" in the current package, so it doesn't know if you wanted to call the builtin pack and unpack or yours. I suggest changing the names of your "pack" and "unpack" subs, which will resolve the ambiguity.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

      mea culpa ... I did indeed have two subs named pack and unpack -- not in the current package, but in a package being used, and written so long ago (last month) I couldn't remember what was in it.

      Thanks for the jolt

        So why did you export pack/unpack then?