Bloodnok has asked for the wisdom of the Perl Monks concerning the following question:
Once more I prostrate myself before you in seeking the answer to a problem that's had me tearing my hair out - I had little enough to start with;-)
The following is, I believe, valid code - but when compiled, generates warnings (c/w a not entirely unexpected error - that I can explain) and for the life of me, I can't see the problem.
The code (from file SVC/DnsEntity.pm):
package SVC::DnsEntity::IPv6; use strict; use warnings; use Carp; my $value; sub new { my $self = shift; bless \( my $scalar ), ref $self || $self; } sub value { $value } package SVC::DnsEntity::IPv4; use strict; use warnings; use Carp; my $value; sub new { my $self = shift; my $_value = shift || croak "No IP address?!"; croak "Invalid IP address" unless $_value =~ /(:?\d{}1,3){4}/; map { croak "Octet out of bounds" unless $_ >= 0 && $_ <= 255 } split /\./, $_value; bless \$_value, ref $self || $self; } sub value { my $self = shift; $$self; } package SVC::DnsEntity; use strict; use warnings; use Carp; use Net::Ping; use Fatal qw/Net::Ping::new Net::Ping::ping/; use SVC::CmdSession; use vars q/%attribs/; BEGIN { no strict q/refs/; map { my $_sub = $_; eval { *$_sub = sub { $attribs{$_sub} } }; croak "Failed to create $_sub - $@" if $@; } qw/hostname ip_address/; } %attribs = ( hostname => undef, type => q/IPv4/, ip_address => undef, pinger => undef, ); my $DEFAULT_TIMEOUT = 1; sub new { my $self = shift; my $args = { ( %attribs ), @_ }; croak "No IP version type - expected IPv[46]" unless $args->{type}->can(qw/new/); my $ver_class = __PACKAGE__ . "\:\:$args->{type}"; warn "ver_class: $ver_class"; $args->{ip_address} = $ver_class->new($args->{ip_address}) if $args->{ip_address}; $attribs{pinger} = Net::Ping->new(); bless \( my %data = %attribs ), ref $self || $self; }
"my" variable $value masks earlier declaration in same scope at SVC/Dn +sEntity.pm line 28. Subroutine new redefined at SVC/DnsEntity.pm line 76. Can't bless non-reference value at SVC/CmdSession.pm line 17.
I would be obliged if someone could point out what I am patently too close to see.
TIA & Rgds,
Update
Many thanx to zentara and Fletch who, between them pointed out just how much I couldn't see the wood for the trees.
|
|---|