Sorry for overr writng
But this pop into my head while walking.
-----Original Message-----
From: "John W. Krahn" <jwkr...@shaw.ca>

Date: Tue, 28 Jul 2009 13:07:38 
To: Perl Beginners<beginners@perl.org>
Subject: Re: Subroutine foo redefined a bar


Dermot wrote:
> 2009/7/28 John W. Krahn <jwkr...@shaw.ca>:
> 
> Thanx for gettig back to me.
> 
>> Which error?  Copy and paste the error message you are receiving.
> 
> Opps sorry. Here you are. Every time I start the httpd I get
> 
>  Processing config file: /etc/apache-modperl/conf.d/dev_vhost.conf
> Subroutine addItemsToBasket redefined at
> /export/web/lib/MyApp/Basket.pm line 347, <DATA> line 238.
> Subroutine addItemToBasket redefined at
> /export/web/lib/MyApp/Basket.pm line 355, <DATA> line 238.

That message means that you have defined two addItemsToBasket 
subroutines and also two addItemToBasket subroutines in the current package.

You could be importing a module before MyApp::Basket that has these 
subroutines or they could be defined in the main package.

Why are you reading from your program file while importing modules?


>>> This offending code reads a like this:
>>>
>>>
>>> package Some::Pack;
>>> ..
>>> ..
>>>
>>>
>>> sub addItemsToBasket {
>>>  my ($bid, $items) = @_;
>>>        foreach my $i (@items) {
>> Is @items global?  Because you are not declaring it in lexical scope.
> 
> Another opps,  that's a typo sorry. It should read
>                foreach my $i ( @{$items} ) {
> 
>>>            addItemToBasket($bid, $i);
>>>        }
>>> }
>>>
>>> sub addItemToBasket {
>>>   my ($bid, $item) = @_;
>>>   ...
>>>   ...
>>>   # do some work that adds item
>>>  }
>> That is very confusing.  Why do you have two subroutines with almost
>> identical names and almost identical functionality?
> 
> Yes, in my defence, I found it this way. That's why I am nervous of
> removing one or the other. The singular function (addItemToBasket)
> expects a single scalar argument in $item. The plural
> (addItemsToBasket) expects an arrayref in $item. It loops through and
> passes the basket_id ($bid) and the item ($i) to the singular
> function.
> 
>>> addItems expects an array ref, addItem expects a scalar. I'm sure it's
>>> possible to merge the 2 subroutines with the use of `wantarray`
>> wantarray() is used for the return value of a subroutine, not for the
>> arguments a subroutine accepts.
> 
> I see.
>>> or `ref` to see what the 2nd argument is.
>> Why not just pass a scalar and a list?
> 
> Because there is a lot of code and I won't know, until it throws an
> error, if there is existing code that passes scalar directly to the
> singular function. I'm just worried that I break some existing
> functions.

How about something like this:

sub addItemsToBasket {
     my $bid = shift;
     my $items = ref $_[0] ? $_[0] : \...@_;




John
-- 
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to