in reply to Initialize variable in BEGIN
Using -n just complicates things for nothing.
With -n:
#!/usr/bin/perl -n use strict; use warnings; our $total; BEGIN { $total = 0; } $total += $_; END { print $total, "\n"; }
Without -n:
#!/usr/bin/perl use strict; use warnings; my $total = 0; while ( <> ) { $total += $_; } print $total, "\n";
One is clearly a million times better.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Initialize variable in BEGIN
by pfaut (Priest) on Apr 30, 2025 at 17:02 UTC | |
by ikegami (Patriarch) on Apr 30, 2025 at 23:54 UTC |