From: "Moreno, Javier \(GXS, Softtek\)" <[EMAIL PROTECTED]>
> I have browsed through the documentation and through responses to this
> list. I always see 'use strict' as a best practice for catching
> errors. But I always get a whole lot of them because I usually do not
> scope variables, the most I do is 'my' when they are only used on a
> subroutine. So is there a tutorial some place I can take a look at to
> properly scope variables? I mean, my code works, but I am worried that
> all the variable scoping errors will affect my program at some point,
> like a performance hit or something of the sort. Can you provide some
> more insight about this? What is the best way to code to achieve
> maximum performance? Do 'use strict' and adhere to the way of coding I
> keep seeing on perl documentation? Please advise.

See "Coping with Scoping" - by MJD
http://www.plover.com/~mjd/perl/FAQs/Namespaces.html

I'd say in short:
Keep the scope of variables as small as possible.

If you use a for/foreach loop do it like this:
        foreach my $item (@array) {
or
        for (my $i=0; $i < 100; $i++ {

If you need a "temporary" variable used only in a few lines, without 
any meaning outside, create a block and scope the variable just to 
those lines:
        {
                my $str = $line;
                $str =~ s/^\s+//;
                $str =~ s/\s+$//;
                @words = split /\s+/, $str;
        }

Or even like this:
        my @words = do {
                my $str = $line;
                $str =~ s/^\s+//;
                $str =~ s/\s+$//;
                split /\s+/, $str;
        };


Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery

_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
  • variables? Moreno, Javier \(GXS, Softtek\)
    • Jenda Krynicky

Reply via email to