• Doubt about the operator //

    From gamo@21:1/5 to All on Sat Feb 13 02:42:06 2021
    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";'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Athanasius@21:1/5 to gamo on Sat Feb 13 14:41:23 2021
    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 <°(((><

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From gamo@21:1/5 to All on Sat Feb 13 05:58:19 2021
    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;

    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,


    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.

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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rainer Weikusat@21:1/5 to gamo on Sun Feb 14 15:50:01 2021
    gamo <gamo@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>;

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)