bairdbe has asked for the wisdom of the Perl Monks concerning the following question:

I'm learning perl 5 by working through Learning Perl 6th ed. Why does removing line "Use 5.010;" break this? I'm use 5.18.2 of the compiler.
#!/usr/bin/perl use strict; use warnings; use 5.010; running_sum( 5, 6 ); running_sum( 1..6 ); running_sum( 4 ); sub running_sum { state $sum = 0; state @numbers; foreach my $number ( @_ ) { push @numbers, $number; $sum += $number; } say "The sum of (@numbers) is $sum"; }

Replies are listed 'Best First'.
Re: why does removing "use 5.010;" break this?
by 1nickt (Canon) on Oct 29, 2017 at 03:56 UTC

    Hi, please put your code inside code tags.

    You can't use state or say as you have your code written, if you take out the use 5.010 statement.

    As the documentation for state says:

    state is available only if the state feature is enabled or if it is prefixed with CORE:: . The state feature is enabled automatically with a use v5.10 (or higher) declaration in the current scope

    So you need to do one of:

    • use 5.010;
    • use feature 'state';
    • CORE::state $foo = 'bar';

    Hope this helps!


    The way forward always starts with a minimal test.
Re: why does removing "use 5.010;" break this?
by haukex (Archbishop) on Oct 29, 2017 at 06:45 UTC

    say and state are "features" that need to be explicitly enabled, either with use feature qw/say state/;, but also use with only a version number will enable the "feature bundle" associated with that Perl version, which for v5.10 happens to be "say state switch" (Update: Forgot array_base, since it wasn't added until v5.16, thanks for the reminder ikegami).

Re: why does removing "use 5.010;" break this?
by ikegami (Patriarch) on Oct 29, 2017 at 18:10 UTC
    use 5.010;
    is equivalent to
    BEGIN { $] >= 5.010 or die "Perl v5.10.0 required--this is only $^V, stopped"; } use feature qw( :5.10 );
    which is equivalent to
    BEGIN { $] >= 5.010 or die "Perl v5.10.0 required--this is only $^V, stopped"; } use feature qw( array_base say state switch );

    Your program relies on the say and state features being activated, so if you remove use 5.010;, you need to add use feature qw( say state ); instead.

    Note that those features were introduced in 5.10, so switching wouldn't gain you anything.