On Sat, 03 Apr 2004 11:49:02 -0800
"R. Joseph Newton" <[EMAIL PROTECTED]> wrote:

> > If one wanted to write a function that used either the given
> > argument or$_ how would you do that?
> 
> Don't
> 
> >   myfunc($myvalue);
> > or
> >   myfunc; #uses the current value of $_
> >
> > sub myfunc {
> >
> >    my $func_arg = shift || 'some constant default value'; # you
> >    wouldn't just
> > do '|| $_;' would you?
> 

> Be careful about what you ask for.  While there is a good use for both
> the loop variable $_ and default values for certain cases where there
> may or may not be an argument offered, both approaches also present
> some serious dangers.  It is much better to explicitly pass arguments.
>  Dependency on magic effects can make
> your code very fragile and very hard to debug.

As a responder to the original question and subsequently having a bug in
my response pointed out to me, I agree with Joseph.

First of all function coding becomes more complicated when trying to
set defaults and the code becomes harder to read. Perl is much like C -
you can do a lot of neat and clever stuff, but you can also easily hang
yourself. I can see Josephs point.

My general rule of thumb is if you cannot come back to a piece of code
you wrote a month ago and understand what it does, then you are probably
being overly clever.

I was too clever in my original response, since I had never done such a
thing in my own Perl coding and I was curious enough to come up with
what turned out to be a buggy solution. Live and learn.

-- 
Smoot Carl-Mitchell
Systems/Network Architect
email: [EMAIL PROTECTED]
cell: +1 602 421 9005
home: +1 480 922 7313

-- 
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