Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Initialize variable in BEGIN

by Fletch (Bishop)
on Apr 30, 2025 at 16:43 UTC ( [id://11164864]=note: print w/replies, xml ) Need Help??


in reply to Initialize variable in BEGIN

For stuff like this I kind of cheat and use -M'5;my $total = 0;' instead (although I wouldn't just for zero since undef does the right thing anyhoo). That along with the eskimo kiss trick (closing the implicit -n or -p while block with }{ ; using -MO=Deparse can show what you're really getting) lets you handle most things.

perl -F, -M'5;my $t = 0' -lanE '$t += $F[2];}{ say $t' some_file.csv

Edit: And I'd never use -n in a shebang line, just when trying for a (too) clever / short one-liner. If you're not golfing or sticking this in the middle of a pipeline it's much better to be explicit in your script and not need to bother with tricks to get reasonable scoping.

The cake is a lie.
The cake is a lie.
The cake is a lie.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (1)
As of 2026-05-10 21:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    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.