Re: Directory/file browser as TreeView



* Ratcliffe, Jeffrey (Peters) <Jeffrey Ratcliffe External eads com> [2006-10-04 15:50]:
31:  $process->( $pathname, sort +( no_upwards @content ) );

What's the "+" for?

I, too, had no idea what this was for. Is there another way of
writing this in one line?

Hmm, there actually is. The following parses the way it needs to:

    sort &no_upwards( @content );

However, I would never be able to remember what special
exceptions apply to the special magic of `sort`. So in order to
be able to read the code when I get back to it in 6 months,
I would still use the plus, which is a construct I am completely
familiar with. Itâs useful in many different contexts and follows
a very simple rule, so itâs easy to commit to memory.

Of course in this case you can just as well use curlies to
disambiguate. I just prefer this notation for my own
(irrelevant

I'm being dumb. How do you can you rewrite this with curlies?

I was talking about `map`. You can write

    %hash = map +( $_ => func( $_ ) ), @list;

as

    %hash = map { $_ => func( $_ ) } @list;

and in fact I know a lot of people *always* write `map` that way
because of the problems with parens.

But it occurs to me that there is a way to write the `sort` with
curlies:

    sort do { no_upwards @content }

That works as it should. Itâs just not obvious why you put the
seemingly pointless `do BLOCK` in there, whereas with the plus
sign, I always immediately know âthis is there because Perl
doesnât disambiguate those parens in the right wayâ by sheer
virtue of what the plus is for.

Yes. The prototype lets you write

    someutil( sub { ... }, $more, @args );

a bit shorter as

    someutil { ... } $more, @args;

Do you pass a sub to a sub like this often? I'd not seen this
before either. I haven't been able to find anything in perldoc
about it. Is there any documentation anywhere?

Exactly what are you referring to? If youâre asking about whether
I use the `&` protoype much, then no, I donât. youâre asking
about passing anonymous subs in general, then the answer is
absolutely, I do that *loads*. I couldnât live without it. I use
the prototype when the sub in question has characteristics of
a control structure.

The documentation you are looking for is in perlsub, under
Prototypes.

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]