Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: A short whishlist of Perl5 improvements leaping to Perl7

by Eily (Monsignor)
on Nov 24, 2020 at 09:19 UTC ( #11124117=note: print w/replies, xml ) Need Help??


in reply to A short whishlist of Perl5 improvements leaping to Perl7

I mean 'my' becomes redundant, isn't it?
Not really, it lets perl catch typos, for example if you have $the_variable_I_want_to_check = 0; somewhere in your code and then $the_varaible_I_want_to_check = 1; later, using strict and my will raise an error at compile time. Python would just silently allow it.

Also having automine would mean that using perl 5 code that wasn't written with strict would compile, but turn package variable into lexicals. So it would still compile but completely change and break the behaviour. Any changes introduced by perl 7 that are not backward compatible should be fatal, not silent.

Replies are listed 'Best First'.
Re^2: A short whishlist of Perl5 improvements leaping to Perl7
by eyepopslikeamosquito (Bishop) on Nov 24, 2020 at 11:07 UTC

    Yes, simple lexical scoping (a variable is known from its point of declaration to end of scope) and deterministic destructors are two things I find enjoyable when using both Perl and C++ ... and I feel confused and frustrated when forced to use the ugly and complex Python scoping workarounds introduced with the global and nonlocal (sic) keywords.

    To give an illustrative example, Python happily compiles and runs the typo-riddled program below, printing vanilla:

    icecream_flavour = 'vanilla' # ... if 1: icecream_flavor = 'chocolate' print(icecream_flavour)
    while Perl detects the typo at compile-time:
    use strict; my $icecream_flavour = 'vanilla'; # ... if (1) { $icecream_flavor = 'chocolate'; } print $icecream_flavour;
    with the error message:

    Global symbol "$icecream_flavor" requires explicit package name (did you forget to declare "my $icecream_flavor"?) at t1.pl line 5.
    Execution of t1.pl aborted due to compilation errors.

    Update: As pointed out by LanX below, this is an unfortunate example of Python violating the Zen of Python, specifically "Explicit is better than implicit". I feel it further violates "Beautiful is better than ugly" and "Simple is better than complex" and "Errors should never pass silently" and "If the implementation is hard to explain, it's a bad idea".

    See also (further update):

      It took me ages to understand why and how to use the with keyword in python, until I realized it was an at attempt at recreating block-scoped variables.

      I think I already had a similar discussion where LanX pointed out that python variables are lexically scoped as well, but the scope they are lexically bound to is the function, not the block. (perl decided to do block-scoped variables, but you could also imagine limitting the scope to a single expression. For example in push @out, {A => $one, b => $two} while my ($one, undef, $two) = some_function())

        Yes, Python doesn't have block scopes.

        It has 4 scope levels° - it took me a while to understand them - and function scope is the smallest.

        To mimic something similar to blocks you'll need again a named function in Python, since "lambdas" (aka anonymous subs in Perl) are limited to one statement only. Python compensates it by allowing nested named functions, leading to enclosed scope. *

        > push @out, {A => $one, b => $two} while my ($one, undef, $two) = some_function())

        This is certainly not doing what you expect, those $one and $two variables are different.

        DB<17> @out=() DB<18> $x=2; push @out, { A =>$one } while my $one = $x-- DB<19> x @out 0 HASH(0x31f07b8) 'A' => undef 1 HASH(0x31f0410) 'A' => undef DB<20> @out=() DB<21> $x=2; push @out, { A =>$one } while $one = $x-- DB<22> x @out 0 HASH(0x32023b0) 'A' => 2 1 HASH(0x3202428) 'A' => 1 DB<23>

        Please keep in mind that declarations are only effective for following statements.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

        °) Local, Global, Built-in, Enclosed

        UPDATE

        *) remember Py's mantra about "explicit is better than implicit"? Perl's scoping rules are explicit ones here (and easier to understand)

Re^2: A short whishlist of Perl5 improvements leaping to Perl7
by ikegami (Pope) on Nov 27, 2020 at 00:17 UTC

    The system works pretty well in Python. But Perl isn't Python. One notable difference would have a major effect:

    The system applies checks to rvalues. And while function arguments are rvalues in Python, sub arguments are lvalues in Perl. Same for method calls.

    Were the model adopted in Perl, typos in sub arguments wouldn't be caught, and could hide typos outside of sub arguments. This would severely harm the efficacity of the system because a large amount of code consists of sub and method calls.

Re^2: A short whishlist of Perl5 improvements leaping to Perl7 (used only once)
by LanX (Cardinal) on Nov 27, 2020 at 18:30 UTC
    > Not really, it lets perl catch typos, for example if you have $the_variable_I_want_to_check = 0; somewhere in your code and then $the_varaible_I_want_to_check = 1; later, using strict and my will raise an error at compile time. Python would just silently allow it.

    should be mentioned that Perl has an "used only once" warning, I'm surprised that python doesn't.

    > Also having automine would mean that using perl 5 code that wasn't written with strict would compile, but turn package variable into lexicals.

    automine or whatever we'd call it must obviously be a pragma extending strict, not a default feature. New pragmas breaking old code is no surprise.

    update

    looks like used-only-once gets deactivated with explicit declaration

    #use strict; use warnings; my $the_variable_I_want_to_check = 0; # somewhere in your code and then $the_varaible_I_want_to_check = 1;

    C:/Perl_524/bin\perl.exe -w d:/tmp/pm/t_typo.pl Name "main::the_varaible_I_want_to_check" used only once: possible typ +o at d:/tmp/pm/t_typo.pl line 8.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      "looks like used-only-once gets deactivated with explicit declaration"

      Lexical variables and variables beginning with an underscore never trigger that warning.

      should be mentioned that Perl has an "used only once" warning
      True, but (in the context of this thread) even that would only save you from unique typos. Make the same typo twice and the typoed name is no longer "used only once".
        Sure, but there is always a risk assessment in life ...

        ... there is no vaccination against car accidents!

        On a side note: Repeated typos happen for me when I use auto-completion in my IDE. But if I already have an IDE I can run a linter on the fly warning me from names with close Levenshtein distance.

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2021-06-15 02:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (66 votes). Check out past polls.

    Notices?