develooper Front page | perl.perl5.porters | Postings from June 2015

Re: scalar sort $scalar

Thread Previous | Thread Next
From:
Ed Avis
Date:
June 30, 2015 14:23
Subject:
Re: scalar sort $scalar
Message ID:
loom.20150630T161306-528@post.gmane.org
I think that split-as-lvalue would have to be restricted to splitting on
fixed strings, not regular expressions.

For 'applying index() to a list' I meant something like this:

    my @a = ('h', 'e', 'l', 'l', 'o');
    my @b = ('e', 'l');
    my $pos = index \@a, \@b;          # $pos = 1

Clearly in this case I could have just made them strings and used the
normal index(), since each element is a single character.  But if it were
not, index() on lists would still let you find the position of one list
if contained inside another.

Similarly I sometimes wish for regular expressions on lists.

    my @path = split m{/}, $filename;
    if (@path =~ m{ ( '..' + ) }magic) {
       my @captured = $1;
       say "found sequence of one or more .. path elements: @captured";
    }

This is a trivial example, but with capturing and backreferences you could
express useful properties of a list in a fairly declarative way.

Now, whether these are useful things to have in core is another matter.
When you write a Perl regular expression you are plugging in to some highly
optimized string handling code, which is carefully written to handle Unicode
correctly.  If processing a list then the advantage of a builtin versus
ordinary Perl code (or a fairly simple XS extension) is less, and the
Unicode issue doesn't come up in the same way.

Although I appreciate the logical consistency of languages like Haskell
which treat a string as a list of characters, so any operation like 'length'
is the same name for strings and lists, it's not terribly necessary to have
that in Perl.

Zefram notes that Perl's idea of value equality is weak.  I see what he or
she means, but I have never really been bothered by it.  99% of the time I
am happy to use string equality, as 'eq'.  That handles strings and it
handles references, only falling down when you mix the two together or add
floating point numbers to the mix.  (I rarely need to compare two floats for
equality without an explicit epsilon.)

-- 
Ed Avis <eda@waniasset.com>


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About