Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: Opinion: where Perl5 wasn't attractive for me

by SuicideJunkie (Vicar)
on Nov 19, 2014 at 15:23 UTC ( [id://1107777]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Opinion: where Perl5 wasn't attractive for me
in thread Opinion: where Perl5 wasn't attractive for me

Yeah, bad idea.

Consider the two proposed automatic mys in the following code. It avoids an obvious compile time fault ($line), while causing a silent run time fault ($indux).

use strict; use warnings; my $index = 0; while ($line = <>) { frobnicate($indux++, $line); }

Appended Re: LanX's comment

If only applying the my to assignments initializations, then consider this code with the same issues:

use strict; use warnings; my $faultsInARow = 0; while ($line = <>) { if (frobnicate($line)) { $faultsInaRow = 0; #Initializing a new variable by mistake. }else{ $faultsInARow ++; } }

Replies are listed 'Best First'.
Re^4: Opinion: where Perl5 wasn't attractive for me
by LanX (Saint) on Nov 19, 2014 at 16:58 UTC
    The now appended example demonstrates a case where (like mentioned before) block scoping matters.

    Python has no blockscoping, such that the second assignment is not considered an declaration cause the variable has already been intialized in the "wider" scope (file or function)

    Perl has blockscoping such that a command like nonlocal would be needed far too often ... or more "headache" (sic) would be needed figure out a workaround.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

      Considering that this is a question of adding the 'feature' to Perl, the fact that Python doesn't have block scoping doesn't really factor into it.

      Also, note that the second assignment is a typo, and IS actually the first (and only) instance of that specific variable name. (I've added a comment to point it out)

        You're getting it wrong ... comparing and learning from experiences other language designers made is helpful.

        If someone says declaration should work like in Ruby or Python it's valuable to show that those languages pay a prize for it.

        And I don't wanna miss block scope...

        Though I don't see a reason to apologize now... If you are not interested in comparing design, ignore me. :)

        Cheers Rolf

        (addicted to the Perl Programming Language and ☆☆☆☆ :)

      Unless Python also requires that all variable initializations happen before any other statements in a method/function, its lack of block scoping is absolutely irrelevant. It doesn't matter whether the scope of $faultsInaRow is the block, the loop or the method. It should have been caught as a typo, because it was not mean as an initialization. It should have been merely an assignment.

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.

Re^4: Opinion: where Perl5 wasn't attractive for me
by LanX (Saint) on Nov 19, 2014 at 16:12 UTC
    This wouldn't compile when applying Python semantics, cause $indux hasn't been initialized.

    So $indux=0 or my $indux would be needed beforehand to avoid compilation errors.

    There are for sure more cases of uncaught typos in Python than in Perl, which blow at runtime.

    But unfortunately this example doesn't demonstrate them well. :)

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    update

    Update outsorced into proper post... :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-20 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found