Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Mystery of missing functions

by throop (Chaplain)
on Oct 27, 2006 at 20:38 UTC ( #581014=perlquestion: print w/replies, xml ) Need Help??

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

Perl monks, I, a pilgrim, seek deeper insight to things Perl. There are certain operations on arrays for which most programming languages have functions, but for which Perl does not. There are instructions on how to do these things in the perldoc, but they aren't core functions:
  • Getting the maximum or minumum element in an array of numbers;
  • Getting the union, intersection or difference of two arrays.
I expect that this is not an oversight, but is rather part of the Perl philosophy. But I cannot divine why. Will a brother monk instruct me?


Replies are listed 'Best First'.
Re: Mystery of missing functions
by mickeyn (Priest) on Oct 27, 2006 at 21:02 UTC
Re: Mystery of missing functions
by swampyankee (Parson) on Oct 27, 2006 at 21:19 UTC

    For a reason, read List::Util. To quote "List::Util contains a selection of subroutines that people have expressed would be nice to have in the perl core, but the usage would not really be high enough to warrant the use of a keyword, and the size so small such that being individual extensions would be wasteful."


    At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

    —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.
Re: Mystery of missing functions
by philcrow (Priest) on Oct 27, 2006 at 20:56 UTC
    You need to use List::Util it does most of those things.


    Update: read docs more closely.

Re: Mystery of missing functions
by Jenda (Abbot) on Oct 27, 2006 at 21:47 UTC

    Apart from List::Utils that others already mentioned I'd like to add that if you want to do union, intersection or difference you should most likely be using a hash, not an array anyway. A hash with empty values, but a hash nevertheless.

    # is element $a in the set? exists $set{$a} # remove $a from the set delete $set{$a}; # add $a to the set $set{$a} = undef; # get the list of elements in the set keys %set # create the union %union = (%set1, %set2); # create the intersection for (keys %set1) { $intersect{$_} = undef if exists $set2{$_}; } # create the difference for (keys %set1) { $diff{$_} = undef unless exists $set2{$_}; } for (keys %set2) { $diff{$_} = undef unless exists $set1{$_}; }

    This will be much quicker than anything you could build with arrays.

Re: Mystery of missing functions
by bart (Canon) on Oct 28, 2006 at 18:42 UTC
    I expect that this is not an oversight, but is rather part of the Perl philosophy.
    Think again. Since the time that Perl 5 came out, not over 10 years ago, maybe one or two new keywords have been added to the language, pos being one of them. For the rest, we are stuck with what was in it from the days of Perl 4. And in those days, there were no modules, and definitely no loadable dynamic extensions, so either something was in the language, or you had to write it in Pure Perl. (Or you had to build a special customized Perl binary, like OraPerl and SybPerl.)

    As a result, there's quite a few of rarely used keywords in the kernel, like gethostbyname which didn't even get its own private entry in perlfunc, while ordinary others like cwd are strangely missing. I'm quite sure that, if they didn't care about backward compatibility, a lot of those old keywords would be kicked out into their own module, such as the oddball functions related to sockets: connect, accept, bind, listen, recv, send, and a couple of even more obscure ones. It's only because perl 4 had them (and had to have them, if you wanted to allow people to write network software in plain Perl), that they're in the language.

    These days, if people want a new keyword, they just ave to write a module (or an XS module, if it has to run very fast). For most applications, that suffices. That's why in the last 10 years only a few new keywords have effectively been added — keywords that likely couldn't have been added to the language in any other way. Like pos.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://581014]
Approved by grep
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2022-12-07 04:19 GMT
Find Nodes?
    Voting Booth?

    No recent polls found