good code vs code that works 
Author Message
 good code vs code that works

is it considered bad practice to use return to jump out of a foreach
loop in a subroutine? my reason for asking:

i'm writing a cgi subroutine (shown below) which compares an email
address against a blacklist text file containing email addresses and the
reasons they were banned. it works but i don't want develop bad habits
as they tend to trip you up further down the road. any pointers
appreciated

also,

doesn't work, are variables local by default when used in a foreach
loop? if not, how can i localise them so that i can cut and paste subs
into other programs without worrying that i might corrupt a global
variable?

thanks

-23

if (&banned($input{'email'})) {
        print "Sorry, this email address has been banned for the following
reason:<br>\n";
        print "$ban_reason<br>\n";
        print "<a href=\"$homepage\" target=\"_top\">back</a><br>\n";

Quote:
}

sub banned {

        open (BANNED, "$banlist") || print "can't open $banlist on line " .
__LINE__ . "<br>\n";

        close (BANNED);


                if ($user =~ /^$checker\t/i) {
                        ($ban_reason) = $user =~ /\t(.*)\n/;
                        return 1;
                }
        }
        return 0;

Quote:
}

the datafile looks like this:





Sat, 04 Aug 2001 03:00:00 GMT  
 good code vs code that works
 [courtesy cc of this posting sent to cited author via email]

In comp.lang.perl.misc,

:are variables local by default when used in a foreach
:loop?

Yes, sometimes, if there's no lexical in scope.
But then it's a local, which is really a global.
This is all explained in the perlsyn manpage.

--tom
--
Real programmers can write assembly code in any language.   :-)  



Sat, 04 Aug 2001 03:00:00 GMT  
 good code vs code that works
[Posted and a courtesy copy mailed.]



Quote:
> is it considered bad practice to use return to jump out of a foreach
> loop in a subroutine?

Not by most programmers.  Only some purists who believe that a function
should have exactly one return statement, and that that should be at the
end of the function, would object to it.  My guess is that such people
would more likely program in Pascal than in Perl.  :-)

Quote:
> also,

> doesn't work, are variables local by default when used in a foreach
> loop? if not, how can i localise them so that i can cut and paste subs
> into other programs without worrying that i might corrupt a global
> variable?

Use 'my' instead of 'local', and the scope of the loop iterator will be
confined to the loop.

--
(Just Another Larry) Rosler
Hewlett-Packard Company
http://www.hpl.hp.com/personal/Larry_Rosler/



Sat, 04 Aug 2001 03:00:00 GMT  
 good code vs code that works

Quote:

> is it considered bad practice to use return to jump out of a foreach
> loop in a subroutine? my reason for asking:

Not at all -- this is frequently useful.  You might consider using
'last' to jump out of the loop, a very common idiom.  (Hell, that's
why Perl *has* 'last'!)

Quote:
> i'm writing a cgi subroutine (shown below) which compares an email
> address against a blacklist text file containing email addresses and the
> reasons they were banned. it works but i don't want develop bad habits
> as they tend to trip you up further down the road. any pointers
> appreciated

Don't worry, there are *plenty* of other bad habits you can pick up.
Jumping out of a loop is *nothing*.  ;-)

Quote:

> also,

> doesn't work, are variables local by default when used in a foreach
> loop? if not, how can i localise them so that i can cut and paste subs
> into other programs without worrying that i might corrupt a global
> variable?

Ooh, yuck, you should not use local unless you really know why --
generally, you only want it on Perl's builtin "special variables".  For
your own variables, use "my".  And "use strict" to make *sure* that you
use "my".

Speaking of which:


*does* work, at least since Perl 5.004.  Very handy idiom, and not a bad
habit at all.

        Greg
--

Corporation for National Research Initiatives    
1895 Preston White Drive                      voice: +1-703-620-8990 x287
Reston, {*filter*}ia, USA  20191-5434               fax: +1-703-620-0913



Sat, 04 Aug 2001 03:00:00 GMT  
 good code vs code that works
: is it considered bad practice to use return to jump out of a foreach
: loop in a subroutine? my reason for asking:

   Not generally.

   It kinda depends on what it is that you are looping over though.

   while ( /(\d+)/g ) {
     return if $1 eq 666; # leaving a //g match before it gets to
                          # the end can sometimes cause problems
   }


: doesn't work, are variables local by default when used in a foreach
: loop?

   Well, since the Fine Manual says they are, then I would tend
   to believe that they in fact are local.

   :-)

   'perlsyn.pod' (syntax) describes foreach:

---------------------
=head2 Foreach Loops

The C<foreach> loop iterates over a normal list value and sets the
variable VAR to be each element of the list in turn.  If the variable
is preceded with the keyword C<my>, then it is lexically scoped, and
is therefore visible only within the loop.  Otherwise, the variable is
implicitly local to the loop and regains its former value upon exiting
the loop.
---------------------

: if not, how can i localise them so that i can cut and paste subs
: into other programs without worrying that i might corrupt a global
: variable?

   You should always prefer my() over local().

   It is both faster and safer.

   s/local/my/ on all of your programs.

: if (&banned($input{'email'})) {
:       print "Sorry, this email address has been banned for the following
: reason:<br>\n";
:       print "$ban_reason<br>\n";
:       print "<a href=\"$homepage\" target=\"_top\">back</a><br>\n";

   Yuk!

   That is pretty hard to read.

   The next person that has to figure out what that does is going
   to spend several seconds pondering that line, when you could
   have written it the "one second to understand" way.

   That next person may even be you!

      print qq(<a href="$homepage" target="_top">back</a><br>\n);

   Easier to read, and less characters to type to boot.

   A win-win approach.

: }

: sub banned {

                         ^  ^
                         ^  ^ not needed and serve no purpose

:       open (BANNED, "$banlist") || print "can't open $banlist on line " .
                      ^        ^
                      ^        ^ not needed and serve no purpose


   Atta boy, 23.

--
    Tad McClellan                          SGML Consulting

    Fort Worth, Texas



Sat, 04 Aug 2001 03:00:00 GMT  
 good code vs code that works

Quote:



> > doesn't work, are variables local by default when used in a foreach
> > loop? if not, how can i localise them so that i can cut and paste subs
> > into other programs without worrying that i might corrupt a global
> > variable?

> Ooh, yuck, you should not use local unless you really know why --
> generally, you only want it on Perl's builtin "special variables".  For
> your own variables, use "my".  And "use strict" to make *sure* that you
> use "my".

ok, i went back and read up on local and my, i thought they were the
same thing but i see now that they're not. thanks for the advice :)

-23

p.s. thanks to everyone else who answered.



Sun, 05 Aug 2001 03:00:00 GMT  
 good code vs code that works

Quote:



>    Atta boy, 23.


HTH, HAND

Cheers,
Philip



Sun, 05 Aug 2001 03:00:00 GMT  
 good code vs code that works

Quote:


> :
> :       print "<a href=\"$homepage\" target=\"_top\">back</a><br>\n";

>    Yuk!

>    That is pretty hard to read.

>    The next person that has to figure out what that does is going
>    to spend several seconds pondering that line, when you could
>    have written it the "one second to understand" way.

>    That next person may even be you!

>       print qq(<a href="$homepage" target="_top">back</a><br>\n);

>    Easier to read, and less characters to type to boot.

true i guess but i'm probably one of the few people in here programming
perl on a mac using bbedit. the text colouring (invaluable for newbies
like me, spotting almost all of my typos) picks up my version
recognising everything inside the "s as stuff to be printed as it
recognises when you escape a ", your version highlights only the words
"$homapage" and "_top"

for me it's much easier my way. anyone else? well.... i'll burn that
bridge when i come to it :)

-23



Sun, 05 Aug 2001 03:00:00 GMT  
 good code vs code that works
[A complimentary Cc of this posting was sent to 23_skidoo


Quote:
> >       print qq(<a href="$homepage" target="_top">back</a><br>\n);

> >    Easier to read, and less characters to type to boot.

> true i guess but i'm probably one of the few people in here programming
> perl on a mac using bbedit. the text colouring (invaluable for newbies
> like me, spotting almost all of my typos) picks up my version
> recognising everything inside the "s as stuff to be printed as it
> recognises when you escape a ", your version highlights only the words
> "$homapage" and "_top"

Again, CPerl would highlight it correctly (on capable Emaxen).

Ilya



Sun, 05 Aug 2001 03:00:00 GMT  
 good code vs code that works

Quote:
> true i guess but i'm probably one of the few people in here programming
> perl on a mac using bbedit. the text colouring (invaluable for newbies
> like me, spotting almost all of my typos) picks up my version
> recognising everything inside the "s as stuff to be printed as it
> recognises when you escape a ", your version highlights only the words
> "$homapage" and "_top"

> for me it's much easier my way. anyone else? well.... i'll burn that
> bridge when i come to it :)

Possibly. There are as few as 800 MacPerlers worldwide, and only about
six or so post here regularly. This is partly owing to the existence
of the MacPerl pages at <http://macperl.com/>, with the archives of
the MacPerl mailing list.

vim <http://www.vim.org/> also does colour syntax high-lighting, is
available for Mac, unix, a number of other systems and DOS and has
smart indenting superior to BBEdit <http://www.barebones.com>.

Ben.



Mon, 06 Aug 2001 03:00:00 GMT  
 good code vs code that works

Quote:

> >       print qq(<a href="$homepage" target="_top">back</a><br>\n);

> >    Easier to read, and less characters to type to boot.

> true i guess but i'm probably one of the few people in here programming
> perl on a mac using bbedit. the text colouring (invaluable for newbies
> like me, spotting almost all of my typos) picks up my version
> recognising everything inside the "s as stuff to be printed as it
> recognises when you escape a ", your version highlights only the words
> "$homapage" and "_top"

What version of BBEdit are you using??

BBEdit, even in version 4.5 (the current version is 5.0), properly
colors 'qq' in operator blue and '(<a href="$homepage"
target="_top">back</a><br>\n)' in string pink.

--


    /                                  http://www.ziplink.net/~rjk/
perl -e'$_="\012534`!./4(%2`\cp%2,`(!#+%2j";s/./"\"\\c$&\""/gees;print'



Tue, 07 Aug 2001 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. bytecode vs. regular code

2. Postal Code/Zip Code Locations

3. Here's Emacs code to track source code during Perl debugging

4. PERL vs. C/C++ code size factor

5. In search of elegant code - running code within a larger Print stmt

6. regex code evaluation (??{code})

7. Code behaves differently depending on surrounding code

8. lines of code in perl vs. c

9. tying together perl code and C code

10. random code question for this code

11. One liners vs. verbose code

12. Routine to Color-Code Java/C/C++ Code

 

 
Powered by phpBB® Forum Software