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

perldata says there are 3 _basic_ Perl data types. But what would you say are all the Perl data types? I can think of
  1. scalar
  2. array
  3. hash
  4. filehandle
  5. dirhandle
  6. subroutine
  7. typeglob


Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality

Replies are listed 'Best First'.
Re: What are all the Perl data types?
by Joost (Canon) on May 03, 2007 at 10:10 UTC
Re: What are all the Perl data types?
by kyle (Abbot) on May 03, 2007 at 10:52 UTC
    my $regex = qr/x/;

    I know $regex is a scalar, but the thing it refers to should be considered another type (which ref calls "Regexp").

      That's interesting. Perhaps, basically, it's whether or not one of the types above are blessed? Otherwise any blessed scalar is another type? That also makes me wonder if we could count iterators somehow.

      -Paul

        Otherwise any blessed scalar is another type?

        I tend to think that a scalar has a reference to a blessed thing rather than the scalar itself being blessed.

        my $unblessed = {}; my $blessed = $unblessed; bless $blessed, 'Holy::Holy'; printf "%s\n%s\n", ref $unblessed, ref $blessed; __END__ Holy::Holy Holy::Holy

        That's just a nitpick, though.

        I ordinarily wouldn't think of some user-created object as being a separate data type except that they can be overloaded to act just like one. Where does something like Math::BigInt fit in? I think this is a point on which reasonable people can disagree.

        That also makes me wonder if we could count iterators somehow.

        I don't know what you mean by this.

      I know $regex is a scalar, but the thing it refers to should be considered another type (which ref calls "Regexp").

      (Sorry for replying so late!) Indeed the return value of qr is a blessed reference. Thus these "objects" are real objects, and turn out to be autoboxed:

      use YAPE::Regex::Explain; sub Regexp::explain { print YAPE::Regex::Explain->new(shift)->explain; } qr/^.*?Foo(Bar){3,7}\d+/->explain;

      In Perl 6 everything is an object too, but currently this is not the case for simple scalars, arrays, etc. unless you use the very interesting autobox module, which used to require a patch, but doesn't anymore, last I checked.

Re: What are all the Perl data types?
by GrandFather (Saint) on May 03, 2007 at 10:28 UTC

    Do you want to count 'constant' (provided by use constant), or is that some sort of scalar?

    How about a heredoc? Does the entity following __DATA__ or __END__ have a data type?

    Is a list a distinct type from an array?


    DWIM is Perl's answer to Gödel
      My take:

      constants are subroutines returning (lists of) scalars.

      heredocs are plain strings. they just have a different syntax from "normal" quoted strings.

      Everything after (and before) __DATA__ / __END__ is in a file, but everything after __DATA__ or __END__ is not processed until read (via the DATA filehandle, or by opening and reading __FILE__), so it's not perl data.

      A list differs from an array. I'm not sure if you can call a list a data type, in perl, though.

Re: What are all the Perl data types?
by jettero (Monsignor) on May 03, 2007 at 10:42 UTC