вопрос по перлу
http://www.unix.com.ua/orelly/perl/prog3/ch31_09.htm (я эту ссылку нашел после своих постов, когда понял, какие ключевые слова для поиска вводить)
я нашел. эта штука используется для более аккуратной работы с объектами совместно с прагмой fields. просто процитирую книжку:
The fields pragma allows us to declare hash-based objects with a restricted list of attributes.
The returned object reference behaves like a hash, but any attempt to get or set an invalid hash key will provoke a fatal error.
If possible, invalid accesses are checked at compile time and produce syntax
errors; otherwise, they are detected at run time.
Here is another version of the Game::Card class created using the fields pragma:
We can use this object class in code like this:
Notice the typed declaration in this example. This is a hint to the Perl interpreter to tell it that
this variable will be holding an object reference of the specified class. Using this information, Perl can detect hash key accesses through the reference and flag invalid keys as compile-type syntax
errors.
If we leave out the type, the code will still work, but the error will be detected only at run
time.
The typed declaration in the new and initialize methods has the same effect—if we try to assign a hash key that wasn’t declared in the list of fields, Perl will reject it.
можно здесь прочитать: я нашел. эта штука используется для более аккуратной работы с объектами совместно с прагмой fields. просто процитирую книжку:
The fields pragma allows us to declare hash-based objects with a restricted list of attributes.
The returned object reference behaves like a hash, but any attempt to get or set an invalid hash key will provoke a fatal error.
If possible, invalid accesses are checked at compile time and produce syntax
errors; otherwise, they are detected at run time.
Here is another version of the Game::Card class created using the fields pragma:
package Game::Card;
use strict;
use fields qw(name suit _up_sleeve);
sub new {
my $proto=shift;
my $class=ref($proto) || $proto;
my Game::Card $self = fields::new($class);
$self->initialize( @_ );
return $self;
}
sub initialize {
my Game::Card $self = shift;
my ($name, $suit) = @_;
$self->{name} = $name;
$self->{suit} = $suit;
}
We can use this object class in code like this:
#!/usr/bin/perl
# fieldsgamecard.pl
use strict;
use warnings;
use Game::Card;
my Game::Card $card = new Game::Card(Ace => 'Spades');
print $self->{name};
print $self->{number}; # ERROR: nonexistent attribute;
Notice the typed declaration in this example. This is a hint to the Perl interpreter to tell it that
this variable will be holding an object reference of the specified class. Using this information, Perl can detect hash key accesses through the reference and flag invalid keys as compile-type syntax
errors.
If we leave out the type, the code will still work, but the error will be detected only at run
time.
The typed declaration in the new and initialize methods has the same effect—if we try to assign a hash key that wasn’t declared in the list of fields, Perl will reject it.
We have already used the base pragma to define class inheritance, but its true purpose comes
to light when it is used in conjunction with fields. Using it, we can add additional attributes to subclasses of fields-based classes like this:
package Amber::Trump;
use strict;
use base Game::Card qw(location alive);
#!/usr/bin/perl
# fieldssubclass.pl
use strict;
use warnings;
use Amber::Trump;
my Amber::Trump $prince=new Amber::Trump('Corwin');
$prince->{suit} = 'black and silver';
$prince->{location} = 'Earth';
$prince->{alive} = 'yes';
ага спасибо! перл блин, век живи век учи.
ну да. а как выучим, так там и шестой официально выйдет.
Оставить комментарий
rosali
народ, кто понимает что это значит:я чёто все мануалы облазил не могу найти