[Vienna-pm] Type des Rückgabewerts
Peter J. Holzer
hjp at wsr.ac.at
Mon Apr 11 06:38:39 PDT 2005
On 2005-04-11 14:26:45 +0200, Bernd Petrovitsch wrote:
> On Mon, 2005-04-11 at 14:22 +0200, Nagy Wilhelm wrote:
> > Koennt' ihr mir mal kurz helfen?
> >
> > sub waswillstdenn {
> > if (wantarray) {
> > print "du willst ein array\n"}
> > else {
> > print "du willst ein scalar\n"}
> > }
> >
> > my $x = waswillstdenn();
> > my @x = waswillstdenn();
> > my %x = waswillstdenn();
> >
> > kann ich irgenwie herausfinden ob der rückgabewert hash oder liste ist?
>
> Gegenfrage: Gibt es einen signifikanten Unterschied zwischen Hash und
> Array?
In der Verwendung auf jeden Fall. Jenachdem kann es sinnvoll sein, die
gleichen Daten einmal als Hash und einmal als Array zu liefern. DBI
macht das so:
$ary_ref = $dbh->selectrow_arrayref($statement);
$hash_ref = $dbh->selectrow_hashref($statement);
und in meinem eigenen Code finde ich dieses Muster auch immer wieder.
Z.B.: perldoc SysDesc::Volumes:
as_xml
Returns a string with an XML representation of the list of all filesys-
tems
as_array
Returns an array containing info about all Volumes. The elements are
objects of type SysDesc::Volume.
as_arrayref
Returns an arrayref containing info about all Volumes. The elements are
objects of type SysDesc::Volume.
as_hashref
Returns a hashref containing info about all volumes. The keys are the
ids (currently a device name by convention), the values are objects of
type SysDesc::Volume.
Das könnte eine einzelne Methode sein, wenn die den gewünschten Typ aus
dem Kontext festellen könnte.
hp (»cpan -i Want« aufrufend)
--
_ | Peter J. Holzer \Beta means "we're down to fixing misspelled comments in
|_|_) | Sysadmin WSR \the source, and you might run into a memory leak if
| | | hjp at wsr.ac.at \you enable embedded haskell as a loadable module and
__/ | http://www.hjp.at/ \write your plugins upside-down in lisp". --ae at op5.se
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 388 bytes
Desc: not available
Url : http://mail.pm.org/pipermail/vienna-pm/attachments/20050411/2a437c1f/attachment.bin
More information about the Vienna-pm
mailing list