Re: array references

2004-11-02 Thread Mike Rylander
On Mon, 01 Nov 2004 22:30:29 -0700, Michael McDonnell
<[EMAIL PROTECTED]> wrote:
> Eric Lease Morgan wrote:
> 
> >
> > How do I manipulate array references?
> >
> > In a package I'm writing I initialize an array reference (I think)
> > through DBI like this:
> >
> >   $self->{author_ids} = $dbh->selectall_arrayref($query);
> 

Actually, from the examples you listed below you probably want to use

   $self->{author_ids} = $dbh->selectcol_arrayref($query);

(Note that the method is selectCOL, not selectALL)
This will give you the one dimensional array ref that you are looking
for, and '$self->{author_ids}' will equal '[1,2,3,4]'.
> >
> > First of all, what sort of foreach loop can I write to iterate through
> > the contents of $self->{author_ids}?
> 

foreach my $author ( @{ $self->{author_ids} } ) {
print $author;
}
print "\n";

This dereferences the arrayref to treat it like a normal array.

> >
> > Third, if I have a list of integers, how to I assign the items in this
> > list to $self->{author_ids}?
> >
> $self->{author_ids}->[0] = 1;
> $self->{author_ids}->[1] = 2;
> $self->{author_ids}->[2] = 3;
> 
> or
> 
> $self->{author_ids} = [1, 2, 3];

These all work fine now that you have a one dimensional arrayref,
because you called selectcol_arrayref instead of selectall_arrayref.

> I would strongly recommend adding quotation marks to {authors_ids} so
> that it is {'authors_ids'} or defining it as a constant.  The only
> reason why {authors_ids} is equivalent to {'authors_ids'} is because
> PERL is converting it to a constant on the fly for you.  If you
> accidentally picked a name that was already a constant in your namespace
> you might end up with results that are hard to debug (imagine if a
> module put a constant in your name space that you didn't know about).

While not bad advice, this isn't entirely accurate.  The issue is in
fact ambiguity, as Michael alluded to, but it's in the other
direction.  You risk NOT using a constant or sub call where you might
have wanted it.  From the perlref man page:

   A new feature contributing to readability in perl version 5.001 is that
   the brackets around a symbolic reference behave more like quotes, just
   as they always have within a string.  That is,

   $push = "pop on ";
   print "${push}over";

   has always meant to print "pop on over", even though push is a reserved
   word.  This has been generalized to work the same outside of quotes, so
   that

   print ${push} . "over";

   and even

   print ${ push } . "over";

 [cut out irrelevant section]

   Similarly, because of all the subscripting that is done using single
   words, we've applied the same rule to any bareword that is used for
   subscripting a hash.  So now, instead of writing

   $array{ "aaa" }{ "bbb" }{ "ccc" }

   you can write just

   $array{ aaa }{ bbb }{ ccc }

   and not worry about whether the subscripts are reserved words.  In the
   rare event that you do wish to do something like

   $array{ shift }

   you can force interpretation as a reserved word by adding anything that
   makes it more than a bareword:

   $array{ shift() }
   $array{ +shift }
   $array{ shift @_ }

and more specifically in regard to constants, from the constant pragma man page:

   You can get into trouble if you use constants in a context which auto-
   matically quotes barewords (as is true for any subroutine call).  For
   example, you can't say $hash{CONSTANT} because "CONSTANT" will be
   interpreted as a string.  Use $hash{CONSTANT()} or $hash{+CONSTANT} to
   prevent the bareword quoting mechanism from kicking in.  Similarly,
   since the "=>" operator quotes a bareword immediately to its left, you
   have to say "CONSTANT() => 'value'" (or simply use a comma in place of
   the big arrow) instead of "CONSTANT => 'value'".

Hope this helps.

--miker

> 
> --
> Michael McDonnell, GCIA
> Winterstorm Solutions, Inc.
> [EMAIL PROTECTED]
>


Re: array references

2004-11-02 Thread Ed Summers
Hi Eric:

On Mon, Nov 01, 2004 at 10:40:36PM -0500, Eric Lease Morgan wrote:
> In a package I'm writing I initialize an array reference (I think) 
> through DBI like this:
> 
>   $self->{author_ids} = $dbh->selectall_arrayref($query);
> 
> First of all, what sort of foreach loop can I write to iterate through 
> the contents of $self->{author_ids}?

De-reference the array reference, here's one example:

foreach my $id ( @{ $self->{author_ids} } ) {
...
}

> Second, how do I undefine the value of $self->{author_ids}?

$self->{author_ids} = undef;

But if I were you I'd have your constructor initialize the slots that can
contain array references to an empty array reference:

sub new {
my $class = shift;
my $self = bless {}, $class;
$self->{author_ids} = [];
}

The advantage here is that you won't attempt to use undef as an array 
reference somewhere in your code. This is a runtime error in Perl, so 
it can result in an unpredictable program if the code isn't exercised
all the time.

> Third, if I have a list of integers, how to I assign the items in this 
> list to $self->{author_ids}?

$self->{author_ids} = [ @list_of_integers ];

or if you don't mind the list being referenced from two locations:

$self->{author_ids} = [EMAIL PROTECTED];

Hope this helps!
//Ed

-- 
Ed Summers
aim: inkdroid
web: http://www.inkdroid.org

The deeper I go the darker it gets. [Peter Gabriel]




Customising MARC::Lint - reading/parsing rules

2004-11-02 Thread Ian . Hamilton
Does anybody (Andy Lester ?) know of any documentation on the read rules,
parse rules functions in Lint.pm - I've looked at the code which has some
useful comments, but some aspects still escape me.

I'd like to adopt a similar approach as I develop my customised Lint checks
- if any one out there has some examples of a similar OO rule-based approach
or documentation of same I'd be very interested.

Ian H.

PS The MARC modules are really great, not only functionally but also as
examples of Perl OOP style. I'm finding them very useful in my day to day
work.

_ 
Ian Hamilton 
Library Systems Administrator 
European Commission - Directorate General for Education and Culture
EAC D3 - Central Library Unit
 +32-2-295.24.60 (direct phone)
 +32-2-299.91.89 (fax)
http://europa.eu.int/eclas/