http://qs321.pair.com?node_id=1150134


in reply to Re: Declaring with my, assigning, and testing in same line (custom routine)
in thread Declaring with my, assigning, and testing in same line

And for completeness , I'm sure you could also use variable :attributes for such checks.
It's possible, but not straightforward. Attributes alone can't do it, because they don't happen at runtime, so they don't see the assigned values:
my @v :Nonempty = keys %h;

Nonempty has no acces to the keys of %h. Fortunately, you can use attributes with tie. It's still not easy, though: the constructor (TIEARRAY) doesn't see the assigned values, either. STORE sees them, but it's not called when the right hand side list is empty! Fortunately, there's one method that gets called in any assignment: EXTEND:

#!/usr/bin/perl use warnings; use strict; { package Array::Nonempty::Attr; use Attribute::Handlers; sub Nonempty :ATTR(ARRAY) { tie @{ $_[2] }, 'Array::Nonempty' } } { package Array::Nonempty; use Tie::Array; use parent -norequire => 'Tie::StdArray'; use Carp; sub EXTEND { my ($self, $size) = @_; croak "Cannot be empty" if 0 == @$self && 0 == $size; $self->SUPER::EXTEND($size) } } use parent -norequire => 'Array::Nonempty::Attr'; my %hash_ok = ( answer => 42 ); my %hash_empty = (); my @keys_ok :Nonempty = keys %hash_ok; my @keys_empty :Nonempty = keys %hash_empty;

You can add other methods to check they wouldn't help you:

use Data::Dumper; sub STORE { warn Dumper('store', \@_); shift->SUPER::STORE(@_) } sub TIEARRAY { warn Dumper('tie', \@_); shift->SUPER::TIEARRAY(@_) } sub CLEAR { warn Dumper('clear', \@_); shift->SUPER::CLEAR(@_) }

To create an array that can never be empty, you'd have to implement SHIFT, POP, and SPLICE, too.

sub SPLICE { my $self = shift; $self->SUPER::SPLICE(@_); croak "Cannot be empty" if 0 == @$self } sub SHIFT { my $self = shift; croak "Cannot be empty" if 1 == @$self; $self->SUPER::SHIFT(@_) } sub POP { my $self = shift; croak "Cannot be empty" if 1 == @$self; $self->SUPER::POP(@_) }
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,