in reply to Re: Initialize variable in BEGIN
in thread Initialize variable in BEGIN

It might make more sense if expressed as a one-liner.

$ for i in {1..10}; do echo $i; done | perl -ne 'BEGIN {our $prod = 1; +} $prod *= $_; END { print $prod,$/; }' 3628800
90% of every Perl application is already written.
dragonchild

Replies are listed 'Best First'.
Re^3: Initialize variable in BEGIN
by ikegami (Patriarch) on Apr 30, 2025 at 23:54 UTC

    I wasn't saying that BEGIN is never useful. I was talking about in the situation at hand.

    But even in your entirely different scenario, does it really make sense?

    perl -nle'BEGIN { $prod = 1; } $prod *= $_; END { print $prod; }'

    vs

    perl -le'$prod = 1; $prod *= $_ while <>; print $prod;'