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

Dear Perlmonks, it was one year and I half that I did not program, and now that I am back at writing something I see an annoying behaviour in my Perl installation. I was trying these simple instructions, which are in a style that I happen to like:
no strict; no warnings; my @things = ( 1, 2, 3, "and", 4, 5); my @bucket; my $counter = 0; foreach my $elt ( @things ) { if ( $elt ne "and" ) { push ( $elt, @{ $bucket[$counter] } ); } if ( $elt eq "and" ) { $counter++; } }
And I receive the following response: "Experimental push on scalar is now forbidden at ... line 7582, near "} )" Execution of ... aborted due to compilation errors." I must confess that I feel tutored to follow somebody else's way here. I am not asking what I should do to obtain my goal here. I am just asking: what is happening? Why should be this strictness an improvement? Thank you in advance for your attention - Gian Luca

Replies are listed 'Best First'.
Re: "Experimental push on scalar is now forbidden"
by eyepopslikeamosquito (Archbishop) on Dec 19, 2021 at 14:10 UTC
Re: "Experimental push on scalar is now forbidden"
by GrandFather (Saint) on Dec 19, 2021 at 22:29 UTC
    no strict; no warnings;

    There is your problem right there - an attitude problem. EvenEspecially very experienced programmers recognize that they fat finger stuff or even experience brain fade at times and use strictures to catch their momentary lapses before too much trouble ensues. Always use strictures (use strict; use warnings; - see The strictures, according to Seuss).

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
      Thank you for your observations. But I do not think that it is a matter of attitude. I would define it as a matter of temperament, or of view of the world. I am perfectly willing to pay the price of nostrictness nowarningness. Indeed, I am saddened when I fire up, say, re.pl, I input something like:
      $a = $b + $c
      and get the response:
      Compile error: Global symbol "$c" requires explicit package name (did +you forget to declare "my $c"?) at (eval 323) line 5. BEGIN not safe after errors--compilation aborted at (eval 323) line 5.
      Is this a big problem for me? Not at all, as soon as I am allowed to begin a program with "no strict; no warnings;". Thank you for your understanding and your willingness to be of help. Best regards Gian Luca

        I am saddened when I fire up, say, re.pl, I input something like: $a = $b + $c ...

        You really shouldn't be using $a and $b like this. :) As noted in perlvar, these two variables are treated specially by Perl. Though fine in sort blocks (for which they were designed), you should avoid them in regular code so as to avoid unnecessary grey hairs, and wasting time, for example wondering why on earth did Perl complain about $c in your sample code, but not $a or $b?

        But I do not think that it is a matter of attitude. I would define it as a matter of temperament, or of view of the world. I am perfectly willing to pay the price of nostrictness nowarningness.

        While Larry might applaud your individualism (he invented TMTOWTDI after all), I don't like your chances of finding teammates willing to embrace your view of the world. At least, your nostrictness nowarningness world view would last about 5 nanoseconds on any team I've ever worked in. :)

        To illustrate where I'm coming from see: Why Create Coding Standards and Perform Code Reviews?

        Update:: Found a Perl Monk who'd be delighted to work in your team.

        I don't know what "I am perfectly willing to pay the price of nostrictness nowarningness" means? For me that would mean less reliable and harder to debug code. I like code that works and anything that furthers that objective is, in my opinion, good. I personally don't enjoy spending hours finding a simple typographical mistake.

        I was also taken aback by "Experimental push on scalar is now forbidden at ... line 7582, near "} )". 7K+ lines of Perl is a heck of a lot of code in a single compilation module! There are ways to separate this into logical units. But this is a forum of the willing. If you ever desire to know how to do this, ask. The Monks will help.

Re: "Experimental push on scalar is now forbidden"
by eyepopslikeamosquito (Archbishop) on Dec 20, 2021 at 00:08 UTC

    Given your Perl is rusty, I'd like to suggest some alternatives to the approach you are currently taking.

    From your sample code, I don't like:

    if ( $elt ne "and" ) { push ( $elt, @{ $bucket[$counter] } ); } if ( $elt eq "and" ) { $counter++; }
    because the repeated tests for "and" violate DRY, a fundamental principle of software development.

    Two alternative ways to remedy are given below:

    use strict; use warnings; use Data::Dumper; my @things = ( 1, 2, 3, "and", 4, 5, "and", 7, 8, 9 ); my @bucket; my $counter = 0; foreach my $elt ( @things ) { if ($elt eq "and") { $counter++; } else { push @{ $bucket[$counter] }, $elt; } } print "counter = $counter\n", Dumper(\@bucket);

    which prints:

    counter = 2 $VAR1 = [ [ 1, 2, 3 ], [ 4, 5 ], [ 7, 8, 9 ] ];

    If you are parsing strings, the Perl split function seems well-suited. For example:

    use strict; use warnings; use Data::Dumper; my $things = 'this and that and another brigand and a bandicoot'; my @bucket = split /\band\b/, $things; print Dumper(\@bucket);

    which prints:

    $VAR1 = [ 'this ', ' that ', ' another brigand ', ' a bandicoot' ];

    If you describe your problem in more detail, we may be able to suggest better ways to do it than your current approach.

      Between the two alternatives, I would choose the first one. It is surely more rational than the one I wrote. But I truly think that both are interesting. Thank you very much for your suggestions. Best regards Gian Luca
Re: "Experimental push on scalar is now forbidden"
by Anonymous Monk on Dec 19, 2021 at 16:02 UTC

    You don't say what you are trying to do, so any suggestion I make may be totally off-track. But it looks to me like you have reversed the arguments to push. See $ perldoc -f push (or, if you prefer, the same thing on the web.)

      Thank you very much to both for your replies. ... Of course I had reversed the arguments to push! I am so ashamed. It was some time I did not write anything and it shows. The error is so gross that I was not taking it into account. So, Perl behaves as usual. Problem solved. Next time I will double-check before posting. Thank you again. Best regards Gian Luca
Re: "Experimental push on scalar is now forbidden"
by LanX (Saint) on Dec 19, 2021 at 19:03 UTC
    I think

    push @{ $bucket[$counter] } , $elt ;

    is what you really wanted. :)

      Absolutely! :| :)