Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Initialize variable in BEGIN

by pfaut (Priest)
on Apr 30, 2025 at 17:02 UTC ( [id://11164867]=note: print w/replies, xml ) Need Help??


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;'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11164867]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2025-12-14 23:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (95 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.