Schrift
[thread]7744[/thread]

exists() auf hashref?

Leser: 1


<< >> 5 Einträge, 1 Seite
Froschpopo
 2006-02-28 05:14
#63324 #63324
User since
2003-08-15
2653 Artikel
BenutzerIn
[default_avatar]
ich hab ne $hashref.
kann man da auch nen exists() anwenden?
Ronnie
 2006-02-28 07:07
#63325 #63325
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
[quote=Froschpopo,28.02.2006, 04:14]ich hab ne $hashref.
kann man da auch nen exists() anwenden?[/quote]
Man kann - aber nicht ohne Nebenwirkungen. Siehe http://perldoc.perl.org/functions/exists.html:
Quote
Although the deepest nested array or hash will not spring into existence just because its existence was tested, any intervening ones will. Thus $ref->{"A"} and $ref->{"A"}->{"B"} will spring into existence due to the existence test for the $key element above. This happens anywhere the arrow operator is used (...)
pq
 2006-02-28 14:49
#63326 #63326
User since
2003-08-04
12208 Artikel
Admin1
[Homepage]
user image
naja, jedenfalls ist exists $hash{key} genauso gefahrlos wie exists $hashref->{key}
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. -- Damian Conway in "Perl Best Practices"
lesen: Wiki:Wie frage ich & perlintro Wiki:brian's Leitfaden für jedes Perl-Problem
Ronnie
 2006-02-28 18:55
#63327 #63327
User since
2003-08-14
2022 Artikel
BenutzerIn
[default_avatar]
[quote=pq,28.02.2006, 13:49]naja, jedenfalls ist exists $hash{key} genauso gefahrlos wie exists $hashref->{key}[/quote]
Ja, bei einer Ebene tritt es nicht auf, aber bei zwei Ebenen unabhängig ob mit '->' oder ohne:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my %one = ( color => "red" );
my $two = { color => "blue" };

print "Bogus\n" if exists $one{shape}{box};
print "Bogus\n" if exists $two->{shape}->{box};

print Dumper \%one, Dumper $two;
renee
 2006-03-01 15:39
#63328 #63328
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Um die Autovivikation zu vermeiden muss man dann halt mehrere exists-Prüfungen machen (für die verschiedenen Ebenen)...
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
<< >> 5 Einträge, 1 Seite



View all threads created 2006-02-28 05:14.