Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Doubt about the operator //

7 views
Skip to first unread message

gamo

unread,
Feb 12, 2021, 8:42:12 PM2/12/21
to

Hi, there.

Supose that I have the following lines:

my $l = <FILE0> // <FILE1> // <FILE2>;
chomp $l;

Does that mean that in the first line there are performed
a line read in various files or just the first one, FILE0
if it's true what it returns?

Thanks in advance.


--
http://gamo.sdf-eu.org/
perl -E 'say "Press return to continue";'

Athanasius

unread,
Feb 12, 2021, 11:41:49 PM2/12/21
to
On 13/02/2021 11:42 am, gamo wrote:
>
> Hi, there.
>
> Supose that I have the following lines:
>
> my $l = <FILE0> // <FILE1> // <FILE2>;
> chomp $l;
>
> Does that mean that in the first line there are performed
> a line read in various files or just the first one, FILE0
> if it's true what it returns?
>
> Thanks in advance.
>
>

Hi gamo,

The logical defined-OR operator // is left-associative and
it short-circuits. So, if <FILE0> returns a defined value,
that is assigned to $l and no further lines are read.

But if <FILE0> returns an undefined value, <FILE1> is called.
Again, if it returns a defined value, that is assigned to $l
and the last file is not read; but if it returns an undefined
value, <FILE2> is called and the result of that line-read is
assigned to $l.

See https://perldoc.pl/perlop#Logical-Defined-Or

Hope that helps,

--
Athanasius <°(((><

gamo

unread,
Feb 12, 2021, 11:58:26 PM2/12/21
to
El 13/2/21 a las 5:41, Athanasius escribió:
That's wonderful. It's just the result that I desire, to open
various files and read all lines and files in that order.

Thank you.

Rainer Weikusat

unread,
Feb 14, 2021, 10:50:07 AM2/14/21
to
gamo <ga...@telecable.es> writes:
> El 13/2/21 a las 5:41, Athanasius escribió:
>> On 13/02/2021 11:42 am, gamo wrote:
>>>
>>> Hi, there.
>>>
>>> Supose that I have the following lines:
>>>
>>> my $l = <FILE0> // <FILE1> // <FILE2>;
>>> chomp $l;

[...]

>> The logical defined-OR operator // is left-associative and
>> it short-circuits. So, if <FILE0> returns a defined value,
>> that is assigned to $l and no further lines are read.
>>
>> But if <FILE0> returns an undefined value, <FILE1> is called.
>> Again, if it returns a defined value, that is assigned to $l
>> and the last file is not read; but if it returns an undefined
>> value, <FILE2> is called and the result of that line-read is
>> assigned to $l.

[...]

> That's wonderful. It's just the result that I desire, to open
> various files and read all lines and files in that order.

If you don't mind reading all file data into memory upfront, you could
also use a list of handles evaluate in list conext.

---
my ($fhg, $fhp);

open($fgh, '<', '/etc/passwd');
open($fhp, '<', '/etc/group');

print $_ for <$fhg>, <$fhp>;
0 new messages