Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^13: If you believe in Lists in Scalar Context, Clap your Hands

by mr_mischief (Monsignor)
on Oct 29, 2008 at 14:50 UTC ( [id://720243]=note: print w/replies, xml ) Need Help??


in reply to Re^12: If you believe in Lists in Scalar Context, Clap your Hands
in thread If you believe in Lists in Scalar Context, Clap your Hands

What constitutes a "first-class object" tends to vary by language. Strictly speaking in generic inter-language terminology, arrays and hashes in Perl are not first-class objects by many definitions. One cannot pass a hash or array into a function intact. One cannot return a hash or an array from a function, or assign one hash or array directly to another. Without library support (DBM::Deep or such) the values are what gets copied (with a hash key being a special type of "value" that is the key to another value).

The only first-class objects in a very strict sense of which I can think right now in Perl are scalars, references, typeglobs, and perhaps (classic, blessed reference to something) object instances. I hesitate to include object instances since they are "just blessed references", but when returned or assigned they do carry their class information and instance information with them at the language level.

  • Comment on Re^13: If you believe in Lists in Scalar Context, Clap your Hands

Replies are listed 'Best First'.
Re^14: If you believe in Lists in Scalar Context, Clap your Hands
by chromatic (Archbishop) on Oct 29, 2008 at 17:46 UTC
    One cannot pass a hash or array into a function intact.

    If that were true, push wouldn't work, and you couldn't override CORE::push. You're confusing pass by value/reference with first-classness.

      What happens there is a clever trick of prototypes in Perl 5. It doesn't pass a variable by reference, but passes a reference to the variable that was an argument. There is a difference. It also sidesteps the fact that any aggregate object passed into a subroutine by the default call-by-value would otherwise be flattened to a list. This flattening by the default variable passing mechanism is evidence against calling Perl aggregates first-class objects.

      If what happened was really "pass by reference" in a classical sense, one would be able to operate on the passed variable by the lexical or localized variable's name without regard for the reference, only it would effect the original. That's not what happens. You can force the subroutine, via the prototype mechanism, to take a reference to the argument but you still have to treat it as a reference.

      sub foo (\@) { print pop @{$_[0]}; }; @bar = qw( foo bar ); foo @bar;

      BTW, lists are not precluded from being first-class simply by not being named, either, as JavaFan pointed out as a disqualification earlier. Anonymous lists, anonymous functions, and anonymous closures are first-class objects in some languages.

      As I said before, what constitutes a first-class value can vary by language. In Perl, one could consider arrays and hashes as first-class, but one could make arguments against that classification as well.

        By your argument, C doesn't have first class integer variables either, as you either pass a copy of the value to a function, or the address of the variable.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://720243]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found