title image


Smiley kunterbunte Info ;o)


Hi,



warum verwendest Du kein "use warnings;" in Deinem Skript?



folgendes Skript:

#!/usr/bin/perl

use strict;

use warnings;

my @test = ();

$test[0] = ("bla");

$test[1] = ("bla","sülz");

$test[2] = ("bla","sülz","trallala");

...

print "$test[0]\n";

print "$test[1]\n";

print "$test[2]\n";



ergibt folgende Meldungen:



Useless use of a constant in void context at test line 6.

Useless use of a constant in void context at test line 7.

Useless use of a constant in void context at test line 7.



Schauen wir es uns mal an.

Zeile 6: $test[1] = ("bla","sülz");



Ein Blick auf http://www.perldoc.com/perl5.8.0/pod/perlreftut.html bringt folgende Erkenntnis:

Make Rule 2



[ ITEMS ] makes a new, anonymous array, and returns a reference to that array. { ITEMS } makes a new, anonymous hash. and returns a reference to that hash.



$aref = [ 1, "foo", undef, 13 ];

# $aref now holds a reference to an array



$href = { APR => 4, AUG => 8 };

# $href now holds a reference to a hash





The references you get from rule 2 are the same kind of references that you get from rule 1:



# This:

$aref = [ 1, 2, 3 ];



# Does the same as this:

@array = (1, 2, 3);

$aref = \@array;





The first line is an abbreviation for the following two lines, except that it doesn't create the superfluous array variable @array



Also schreiben wir die betroffenen Zeilen um:

#!/usr/bin/perl

use strict;

use warnings;

my @test = ();

$test[0] = ["bla"];

$test[1] = ["bla","sülz"];

$test[2] = ["bla","sülz","trallala"];



print "$test[0]\n";

print "$test[1]\n";

print "$test[2]\n";



Dieses Skript gibt keine Warnungen mehr aus, dafür haben wir aber nun folgende Ausgabe:



ARRAY(0x8136e5c)

ARRAY(0x8136f64)

ARRAY(0x8146630)



Blicken wir wieder in die oben bereits zitierte Webseite, erfahren wir weiter:

If you try to use a reference like a string, you get strings like



ARRAY(0x80f5dec) or HASH(0x826afc0)



If you ever see a string that looks like this, you'll know you printed out a reference by mistake.



Aha, also müssen wir die Referenzen auf die "inneren" Arrays dereferenzieren. Fix nachgelesen, erfahren wir:



Use Rule 1



If $aref contains a reference to an array, then you can put {$aref} anywhere you would normally put the name of an array. For example, @{$aref} instead of @array.



Here are some examples of that:



Arrays:



@a @{$aref} An array

reverse @a reverse @{$aref} Reverse the array

$a[3] ${$aref}[3] An element of the array

$a[3] = 17; ${$aref}[3] = 17 Assigning an element





On each line are two expressions that do the same thing. The left-hand versions operate on the array @a, and the right-hand versions operate on the array that is referred to by $aref, but once they find the array they're operating on, they do the same things to the arrays.



...



Use Rule 2



${$aref}[3] is too hard to read, so you can write $aref->[3] instead.

...

Most often, when you have an array or a hash, you want to get or set a single element from it. ${$aref}[3] and ${$href}{'red'} have too much punctuation, and Perl lets you abbreviate.



If $aref holds a reference to an array, then $aref->[3] is the fourth element of the array. Don't confuse this with $aref[3], which is the fourth element of a totally different array, one deceptively named @aref. $aref and @aref are unrelated the same way that $item and @item are.



Daraus schliessen wir, dass wir, wenn wir die Array-Inhalte selber wieder sichtbar machen wollen, wir die Ausgabe ebenfalls umschreiben sollten:





#!/usr/bin/perl

use strict;

use warnings;

my @test = ();

$test[0] = ["bla"];

$test[1] = ["bla","sülz"];

$test[2] = ["bla","sülz","trallala"];





print "@{$test[0]}\n";

print "@{$test[1]}\n";

print "@{$test[2]}\n";





Was uns folgende Ausgabe liefert:

bla

bla sülz

bla sülz trallala



Hoffe, Dir wird das Ganze dadurch klarer ...



Doku zu dem Thema:

http://www.perldoc.com/perl5.8.0/pod/perlreftut.html

http://www.perldoc.com/perl5.8.0/pod/perldsc.html

http://www.perldoc.com/perl5.8.0/pod/perllol.html



Greetz,





Linuxer





Wann werden endlich wieder brauchbare Fehlermeldungen Mode?


Alle Angaben ohne Gewähr und auf eine Linux-Umgebung abgestimmt!


Woran liegt es, dass ein Danke oftmals schwerer geschrieben ist als eine bei weitem längere Frage?


http://perldoc.perl.orghttp://search.cpan.orghttp://httpd.apache.orghttp://www.oreillynet.com/linux/cmd/



geschrieben von

Login

E-Mail:
  

Passwort:
  

Beitrag anfügen

Symbol:
 
 
 
 
 
 
 
 
 
 
 
 
 

Überschrift: