[comp.lang.perl] unpack

dcd@tc.fluke.COM (David Dyck) (03/10/91)

unpack("h0","0")'; dumps core on a sun spark,
but unpack("a0","0")'; doesn't.

4.0.beta PL0 and 3.0 PL44

            David Dyck			dcd@tc.fluke.COM
      UUCP: {uunet,uw-beaver,decwrl,microsof,sun}!fluke!dcd

worley@compass.com (Dale Worley) (03/12/91)

It dumps core on Perl 3.0pl44 on a Sun 3 running SunOS 4.0.3 also:

    63> perl-test
    unpack("h0", "0")
    Segmentation fault (core dumped)
    64> 

Dale

Dale Worley		Compass, Inc.			worley@compass.com
--
"The floggings will continue until morale improves."

mayer@sono.uucp (Ronald &) (06/27/91)

Inconsistancy with man page.

Quoting from the man page:
>   Here's a subroutine that does substring:
>                  sub substr {
>                       local($what,$where,$howmuch) = @_;
>                       unpack("x$where a$howmuch", $what);
>                  }
>

however using the builtin:
        print "##",substr('hi',10,10),"##"
gives me "####".
but using the procedure as printed in the man page:
        print "##",&substr('hi',10,10),"##"
dies with an error "x outside of string at - line 3."

Could someone tell me which behavior is 'correct'?

        Ron
        mayer@sono.uucp

[Didn't someone recently summarize part of the perl philosophy as
being "never cause an error; just return null and let the programmer
set up whatever error conditions he wants?]

perl version 4.0 patchlevel 3 - sparc, sunos 4.1.1

merlyn@iWarp.intel.com (Randal L. Schwartz) (06/28/91)

In article <MAYER.91Jun26110104@porky.sono.uucp>, mayer@sono (Ronald &) writes:
| 
| Inconsistancy with man page.
| 
| Quoting from the man page:
| >   Here's a subroutine that does substring:
| >                  sub substr {
| >                       local($what,$where,$howmuch) = @_;
| >                       unpack("x$where a$howmuch", $what);
| >                  }
| >
| 
| however using the builtin:
|         print "##",substr('hi',10,10),"##"
| gives me "####".
| but using the procedure as printed in the man page:
|         print "##",&substr('hi',10,10),"##"
| dies with an error "x outside of string at - line 3."
| 
| Could someone tell me which behavior is 'correct'?

You're gonna hate me for saying that they're both *correct*.

The line from the manpage could be annotated to read "... does
substring for well-behaved arguments:".  You passed substring some of
its exceptional argument values, for which it does defined (and nice)
things, like return null strings.

Pack and unpack, on the other hand, are typically used to transmogrify
data of known shapes and sizes.  A slight difference in philosophy,
and I'm *glad* it's a fatal error to select outside the boundaries.

Yeah, both could fatal, or both could return null, but it's *this* way
instead.  Why do you think it takes a while to become a Perl guru? :-)

print unpack("x24"."aX2"x 24."a",",rekcah lreP rehtona tsuJ")
-- 
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/