in reply to Re^7: Is there a problem with using barewords as filehandles ?
in thread Is there a problem with using barewords as filehandles ?

> File-scope lexicals look like lexicals, but have effectively the same scope as globals. In terms of surprise action-at-a-distance, they carry the same risks.

Nope, again, there are plenty of ways to avoid a lexical to be closed over, like writing the main code after the subs are declared or putting a block around it.

And people who ignore lexical filehandles, don't know about packages either.

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

  • Comment on Re^8: Is there a problem with using barewords as filehandles ?

Replies are listed 'Best First'.
Re^9: Is there a problem with using barewords as filehandles ?
by karlgoethebier (Abbot) on Jul 08, 2020 at 22:12 UTC
    «...writing the main code after the subs are declared or putting a block around it.»

    You mean something like this I guess:

    #!/usr/bin/env perl use strict; use warnings; KARL: { nose(); } sub nose {...;} __END__

    I wrote my code many years like this and I can’t tell how often I’ve been blamed for it.

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      > You mean something like this I guess:

      Almost

      use strict; use warnings; MAIN: { open my $fh,">","filename"; ... nose(); ... } sub nose {...;}

      nose() won't have access to any lexical vars defined inside the main block.

      > I can’t tell how often I’ve been blamed for it.

      It's a variation of putting the main code into a main() sub and calling it from the start.

      I think it's a pattern taken from C and I've seen some monks here advocating it.

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

        Yes. This I meant. Creating a main() sub I always considered to be unperlish. The block is the way to go. And to be honest: Sometimes when I was unsure and/or in a hurry I’ve put another block into a block. With the nice «side-effect» of some nice indention. Best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

        perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re^9: Is there a problem with using barewords as filehandles ?
by jcb (Parson) on Jul 08, 2020 at 01:30 UTC

    If you put a block around a file-scope lexical, it is not a file-scope lexical anymore. Writing the main code after subs are defined is another "just be careful" convention — it works (until the convention is broken), but if you do not accept conventions as legitimate solutions to problems with bareword filehandles, then why do you accept conventions as solutions to problems with lexicals?

    The last issue of programmer ignorance is not a problem that can be solved by changing the language, only by educating the programmers about the language in which they write.

      > If you put a block around a file-scope lexical, it is not a file-scope lexical anymore.

      My point was that the scope of lexical variables can be easily limited.

      You can't our FHs to limit the scope so you'd need different packages.

      > only by educating the programmers about the language in which they write.

      Well we wouldn't need any other languages if programmers were educated enough to use assembler.

      FHs need a lot of extra technique, educating a new programmer about all the tricks needed takes a while, without extra benefit.

      Discouraging their use doesn't mean changing the language.

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

        Well we wouldn't need any other languages if programmers were educated enough to use assembler.

        It's a matter of productivity as well.