Kenton Brede wrote:
> 
> On Sat, Feb 07, 2004 at 07:42:30PM -0000, Rob Dixon ([EMAIL PROTECTED]) wrote:
> >
> >   sub get_uid {
> >     my %list;
> >     @list{map {(split /:/)[2]} <DATA>} = ();
> 
> map reads the UID variables from <DATA> into @list.

Into %list actually.  @list{} is a hash slice.

> What I don't
> understand is "= ()"  My understanding is assignments move from right to
> left so it seems "()" is assigned to "@list{map {(split /:/)[2]} <DATA>}"
> I'm guessing this is how the hash is populated but it looks like "()" is
> undef?

Yes, "= ()" assigns undef for all the keys in %list.

> >     return (grep {not exists $list{$_}} 700 .. 799)[0];
> 
>  From perldoc "grep BLOCK LIST."
>  So grep finds the first "[0]" UID in the hash that doesn't exist in the list
>  700 to 799.

Yes.

> >   }
> >
> > More readably, this does almost the same thing:
> >
> >   sub get_uid {
> >
> >     my %list;
> >
> >     while(<DATA>) {
> >       my $uid = (split /:/)[2];
> >       $list{$uid}++;
> 
> Looks like this increments the next instance of $uid into the hash.

Yes although you don't really need the increment, you just have to
ensure that some value is assigned in order to autovivify the key. 
Since Rob is using exists() you could even assign undef and it would
work.

> >     }
> >
> >     foreach my $uid (700 .. 799) {
> >       return $uid unless exists $list{$uid};
> >     }
> >
> >     return undef;
> 
> I've got a question about this line.  I know it's there if all
> available UIDs are taken, then the sub will return a value.  You've
> chosen to return "undef."  Would it be in proper form to -
> 
> return "No available UIDs in the 700 range." ?
> 
> Maybe a better way of asking the question is, Are there "return"
> guidelines?

Most functions in perl return undef when they encounter an error. 
However it is usually better to use return with no arguments which will
return undef in scalar context and an empty list in list context and
nothing in void context.

> I've perldoc -f and -q and
> searched in the archives and didn't find a whole lot.
> Any doc pointers on this topic would be helpful.

perldoc perlsub


John
-- 
use Perl;
program
fulfillment

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