Re: Raku one-liners posted on Unix & Linux StackExchange

2025-05-27 Thread Sean McAfee
On Mon, May 26, 2025 at 6:50 AM Richard Hainsworth 
wrote:

> My understanding is that there is a default test so that '.' and '..'
> are not returned. So if :test is not specified thats the behaviour you get.
>

The page I linked to earlier doesn't read that way to me at all:

Returns a lazy list of IO::Path objects corresponding to the entries in a
directory, optionally filtered by smartmatching their names *as strings*
per the :test parameter. [...]  Objects corresponding to special directory
entries . and .. are not included.

It certainly seems odd that adding a filter can result in more elements
being returned than without one.

Anyway, I'll file a bug report shortly, to at least fix the documentation.
However, I reported a similar mismatch
 a few years ago and
encountered a lot of resistance, ending with nothing being done, so I'm not
holding my breath.


Re: Raku one-liners posted on Unix & Linux StackExchange

2025-05-26 Thread Richard Hainsworth

On 26/05/2025 12:13, Elizabeth Mattijsen wrote:

On 26 May 2025, at 00:34, Sean McAfee  wrote:

On Sun, May 25, 2025 at 12:32 PM Sean McAfee  wrote:
I'll have to play around with your code a bit. Normally I would use `.dir(test 
=> /:i \.pdf / )`  to pull out PDF files.

Oh yeah, that's a bit better.  I'm not as familiar with the path API as I could 
be.

...Huh.  I was just messing around with the test parameter to dir and noticed something that seems to be a bug.  If you 
call dir without any parameters, the special directories "." and ".." are not returned, as 
mentioned on this page.  But if you pass a test parameter, those directories are eligible to be returned.  For example, 
dir(:test(/./)) returns ".".IO and "..".IO among its results.

Does anyone know if this is the intended behavior?  It doesn't really seem 
useful.

Afaik it is intended behaviour, as they are specifically being added in the case of the 
"test" argument.

https://github.com/rakudo/rakudo/blob/main/src/core.c/Rakudo/Iterator.rakumod#L1524
My understanding is that there is a default test so that '.' and '..' 
are not returned. So if :test is not specified thats the behaviour you get.


Re: Raku one-liners posted on Unix & Linux StackExchange

2025-05-26 Thread Elizabeth Mattijsen
> On 26 May 2025, at 00:34, Sean McAfee  wrote:
> 
> On Sun, May 25, 2025 at 12:32 PM Sean McAfee  wrote:
> I'll have to play around with your code a bit. Normally I would use 
> `.dir(test => /:i \.pdf / )`  to pull out PDF files. 
> 
> Oh yeah, that's a bit better.  I'm not as familiar with the path API as I 
> could be.
> 
> ...Huh.  I was just messing around with the test parameter to dir and noticed 
> something that seems to be a bug.  If you call dir without any parameters, 
> the special directories "." and ".." are not returned, as mentioned on this 
> page.  But if you pass a test parameter, those directories are eligible to be 
> returned.  For example, dir(:test(/./)) returns ".".IO and "..".IO among its 
> results.
> 
> Does anyone know if this is the intended behavior?  It doesn't really seem 
> useful.

Afaik it is intended behaviour, as they are specifically being added in the 
case of the "test" argument.

https://github.com/rakudo/rakudo/blob/main/src/core.c/Rakudo/Iterator.rakumod#L1524

Re: Raku one-liners posted on Unix & Linux StackExchange

2025-05-25 Thread Sean McAfee
On Sun, May 25, 2025 at 12:32 PM Sean McAfee  wrote:

> I'll have to play around with your code a bit. Normally I would use
>> `.dir(test => /:i \.pdf / )`  to pull out PDF files.
>>
>
> Oh yeah, that's a bit better.  I'm not as familiar with the path API as I
> could be.
>

...Huh.  I was just messing around with the test parameter to dir and
noticed something that seems to be a bug.  If you call dir without any
parameters, the special directories "." and ".." are not returned, as
mentioned on this page .  But if you
pass a test parameter, those directories are eligible to be returned.  For
example, dir(:test(/./)) returns ".".IO and "..".IO among its results.

Does anyone know if this is the intended behavior?  It doesn't really seem
useful.


Re: Raku one-liners posted on Unix & Linux StackExchange

2025-05-25 Thread Sean McAfee
On Sat, May 24, 2025 at 2:31 AM William Michels 
wrote:

> Thanks, Sean!
>
> Do any of the various `PDF` modules work to solve your page-counting quest?
>

My task was just a one-off, so I didn't look.  If I were working on a more
permanent program, I'd try to reduce external dependencies in that way.

Similarly, I think exiftool has some option that would print *only* the
metadata I wanted, but it would have taken longer to look it up than to
just process the entire output.


> I'll have to play around with your code a bit. Normally I would use
> `.dir(test => /:i \.pdf / )`  to pull out PDF files.
>

Oh yeah, that's a bit better.  I'm not as familiar with the path API as I
could be.

Yesterday I noticed that there were some duplicate PDFs among some pairs of
directories, so I identified them like so:

.say for sort keys 'dir1'.IO.dir».basename ∩ 'dir2'.IO.dir».basename

It's tempting to golf that down a bit, but it just a one-time need, so...
¯\_(ツ)_/¯


Re: Raku one-liners posted on Unix & Linux StackExchange

2025-05-24 Thread Clifton Wood
My previous message should read ".say for" instead of "say for". Sorry

On Sat, May 24, 2025 at 6:28 AM Clifton Wood  wrote:

> Just for fun, here's a similar one-liner using File::Find and PDF::Class:
>
> # Formatted for clarity;
> use File::Find;
> use PDF::Class; .
> say for find( dir => ".", name => *.ends-with(".pdf") ).map(
>   sub ($_) {
> CATCH { default { return 0 } };
> say "Checking { .absolute }...";
> PDF::Class.open($_).page-count
>   }
> ).sum'
>
> It's a bit larger than the more elegant solution provided by Sean, but it
> does include some nice to haves.
>
>
>
> On Sat, May 24, 2025 at 5:31 AM William Michels via perl6-users <
> [email protected]> wrote:
>
>> Thanks, Sean!
>>
>> Do any of the various `PDF` modules work to solve your page-counting
>> quest?
>>
>> I'll have to play around with your code a bit. Normally I would use
>> `.dir(test => /:i \.pdf / )`  to pull out PDF files.
>> The closest U&L answer posted for your issue might be:
>>
>>
>> https://unix.stackexchange.com/questions/651035/how-to-count-total-number-of-lines-of-all-txt-files/745652#745652
>>
>> Cheers!
>>
>>
>>
>> On May 21, 2025, at 13:08, Sean McAfee  wrote:
>>
>> On Tue, May 20, 2025 at 12:51 PM William Michels via perl6-users <
>> [email protected]> wrote:
>>
>>> If you haven't visited U&L StackExchange, you should! It's much less
>>> 'siloed' than StackOverflow in that an OP might post a question requesting
>>> a bash/sed/awk answer, but other answers are readily accepted (and
>>> upvoted!) as well. Some good answers written in:  bash, zsh, sed, awk, jq,
>>> mlr, Ruby, Perl, and Raku. Check it out!
>>>
>>
>> Cool!  I'll have to drop by sometime.  I've been moving to use Raku in
>> place of shell scripts or commands more often.  A short time ago I wanted
>> to get a list of directories along with the total page count of all PDF
>> files within them, sorted by the page counts, and I was able to do it with
>> a short-ish Raku one-liner (broken into multiple lines here for clarity):
>>
>> .say for sort dir.map: {
>>   run('exiftool', .dir.grep(/'.pdf'$/), :out)
>> .out.slurp.match(:g, /^^ 'Page Count' \N+? <( \d+ $$/).sum => ~$_ if
>> .d
>> }
>>
>> The one thing that's hardest for me to keep in mind is to use "\N"
>> instead of ".", which here silently produces dramatically wrong results.
>>
>>
>>


Re: Raku one-liners posted on Unix & Linux StackExchange

2025-05-24 Thread Clifton Wood
Just for fun, here's a similar one-liner using File::Find and PDF::Class:

# Formatted for clarity;
use File::Find;
use PDF::Class; .
say for find( dir => ".", name => *.ends-with(".pdf") ).map(
  sub ($_) {
CATCH { default { return 0 } };
say "Checking { .absolute }...";
PDF::Class.open($_).page-count
  }
).sum'

It's a bit larger than the more elegant solution provided by Sean, but it
does include some nice to haves.



On Sat, May 24, 2025 at 5:31 AM William Michels via perl6-users <
[email protected]> wrote:

> Thanks, Sean!
>
> Do any of the various `PDF` modules work to solve your page-counting quest?
>
> I'll have to play around with your code a bit. Normally I would use
> `.dir(test => /:i \.pdf / )`  to pull out PDF files.
> The closest U&L answer posted for your issue might be:
>
>
> https://unix.stackexchange.com/questions/651035/how-to-count-total-number-of-lines-of-all-txt-files/745652#745652
>
> Cheers!
>
>
>
> On May 21, 2025, at 13:08, Sean McAfee  wrote:
>
> On Tue, May 20, 2025 at 12:51 PM William Michels via perl6-users <
> [email protected]> wrote:
>
>> If you haven't visited U&L StackExchange, you should! It's much less
>> 'siloed' than StackOverflow in that an OP might post a question requesting
>> a bash/sed/awk answer, but other answers are readily accepted (and
>> upvoted!) as well. Some good answers written in:  bash, zsh, sed, awk, jq,
>> mlr, Ruby, Perl, and Raku. Check it out!
>>
>
> Cool!  I'll have to drop by sometime.  I've been moving to use Raku in
> place of shell scripts or commands more often.  A short time ago I wanted
> to get a list of directories along with the total page count of all PDF
> files within them, sorted by the page counts, and I was able to do it with
> a short-ish Raku one-liner (broken into multiple lines here for clarity):
>
> .say for sort dir.map: {
>   run('exiftool', .dir.grep(/'.pdf'$/), :out)
> .out.slurp.match(:g, /^^ 'Page Count' \N+? <( \d+ $$/).sum => ~$_ if
> .d
> }
>
> The one thing that's hardest for me to keep in mind is to use "\N" instead
> of ".", which here silently produces dramatically wrong results.
>
>
>


Re: Raku one-liners posted on Unix & Linux StackExchange

2025-05-24 Thread William Michels via perl6-users
Thanks, Sean!

Do any of the various `PDF` modules work to solve your page-counting quest?

I'll have to play around with your code a bit. Normally I would use `.dir(test 
=> /:i \.pdf / )`  to pull out PDF files. 
The closest U&L answer posted for your issue might be:

https://unix.stackexchange.com/questions/651035/how-to-count-total-number-of-lines-of-all-txt-files/745652#745652

Cheers!



> On May 21, 2025, at 13:08, Sean McAfee  wrote:
> 
> On Tue, May 20, 2025 at 12:51 PM William Michels via perl6-users 
> mailto:[email protected]>> wrote:
>> If you haven't visited U&L StackExchange, you should! It's much less 
>> 'siloed' than StackOverflow in that an OP might post a question requesting a 
>> bash/sed/awk answer, but other answers are readily accepted (and upvoted!) 
>> as well. Some good answers written in:  bash, zsh, sed, awk, jq, mlr, Ruby, 
>> Perl, and Raku. Check it out!
> 
> Cool!  I'll have to drop by sometime.  I've been moving to use Raku in place 
> of shell scripts or commands more often.  A short time ago I wanted to get a 
> list of directories along with the total page count of all PDF files within 
> them, sorted by the page counts, and I was able to do it with a short-ish 
> Raku one-liner (broken into multiple lines here for clarity):
> 
> .say for sort dir.map: {
>   run('exiftool', .dir.grep(/'.pdf'$/), :out)
> .out.slurp.match(:g, /^^ 'Page Count' \N+? <( \d+ $$/).sum => ~$_ if .d 
> }
> 
> The one thing that's hardest for me to keep in mind is to use "\N" instead of 
> ".", which here silently produces dramatically wrong results.
> 



Re: Raku one-liners posted on Unix & Linux StackExchange

2025-05-21 Thread Sean McAfee
On Tue, May 20, 2025 at 12:51 PM William Michels via perl6-users <
[email protected]> wrote:

> If you haven't visited U&L StackExchange, you should! It's much less
> 'siloed' than StackOverflow in that an OP might post a question requesting
> a bash/sed/awk answer, but other answers are readily accepted (and
> upvoted!) as well. Some good answers written in:  bash, zsh, sed, awk, jq,
> mlr, Ruby, Perl, and Raku. Check it out!
>

Cool!  I'll have to drop by sometime.  I've been moving to use Raku in
place of shell scripts or commands more often.  A short time ago I wanted
to get a list of directories along with the total page count of all PDF
files within them, sorted by the page counts, and I was able to do it with
a short-ish Raku one-liner (broken into multiple lines here for clarity):

.say for sort dir.map: {
  run('exiftool', .dir.grep(/'.pdf'$/), :out)
.out.slurp.match(:g, /^^ 'Page Count' \N+? <( \d+ $$/).sum => ~$_ if .d
}

The one thing that's hardest for me to keep in mind is to use "\N" instead
of ".", which here silently produces dramatically wrong results.