On Mar 27, 2004, at 10:42 AM, WC -Sx- Jones wrote:

2. Your use of local() is scary at best. It looks like a step backwards in Perl history to me. local() is pretty misunderstood to begin with and I don't think we, of all people, should be adding to the problem.


I guess I am think as a brick:

#! /usr/bin/perl -T

use strict;
use warnings;

my $outside = 5;

sub change_5 () {
# *local* is needed to fix this...
  $outside = 7;
  print "Inside sub: $outside\n\n";
}

&change_5();
print "Out of sub: $outside\n\n";

our $alsooutside = 5;

sub change_5a () {
# *local* is needed to fix this...
  $alsooutside = 7;
  print "Inside sub: $alsooutside\n\n";
}

&change_5a();
print "Out of sub: $alsooutside\n\n";

# both vars - (also)outside - came into
# existence by some other means - usage
# is to shorten the example...

The above is a text-book perfect example of the confusions about local(), save that the comments are off. Both "problems" shown above are fixed just fine with my(), instead of local() and there is no advantage to using local() in either case. my() provides the inner scoping, leaving the outer variable untouched. The code doesn't even compile if you use local() on line 10. Try it and find out.


(The taint switch also isn't needed above, nor are the ampersands and I think it would be better to remove them.)

Here's what I consider a correct way to use local():

# $/ exists outside the following method, set to "\n" by perl, or who knows what else

sub read_paragraphs {
        my $file = shift;
        my @paragraphs;
        
        local $/ = '';  # proper use of local(), change $/ in here only

        while (<$file>) {
                chomp;
                push @paragraphs, $_;
        }
        return @paragraphs;
}       # right here, $/ goes back to what it was before

3. You are knowingly encouraging beginners to riddle their code with global variables. I think they can come up with enough bad habits

Not trying to invent anything -- these are real examples I see everyday in the wild...

Yes, as Charles says, that's the problem we are trying to solve. I would rather you were part of the solution than the problem.


James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to