Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^5: Declaring with my, assigning, and testing in same line (attributes)

by choroba (Cardinal)
on Dec 13, 2015 at 17:50 UTC ( #1150166=note: print w/replies, xml ) Need Help??


in reply to Re^4: Declaring with my, assigning, and testing in same line (attributes)
in thread Declaring with my, assigning, and testing in same line

The check would be done only once
My tests show otherwise:
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use Syntax::Construct qw{ // }; { package Array::Nonempty; use Tie::Array; use parent -norequire => 'Tie::StdArray'; use Carp; sub CLEAR { my $self = shift; $self->[0] = []; } sub TIEARRAY { my ($class, $untie) = @_; bless [ [], $untie ], $class } sub EXTEND { my ($self, $size) = @_; croak "Cannot be empty" if 0 == @{ $self->[0] } && 0 == $size; $self->SUPER::EXTEND($size); # Prevent "untie attempted while 1 inner references still exis +t" my $untie = $self->[1]; undef $self; untie @$untie; } } { package UNIVERSAL; use Attribute::Handlers autotieref => { Nonempty => 'Array::Nonemp +ty' }; } my @list = 1 .. 5; for (1 .. 10) { my @check :Nonempty = @list; shift @list; say tied(@check) // 'not tied'; say "@check"; }
Wrong I was, young padawan. ;-)
There are ways how not to be wrong.
($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,

Replies are listed 'Best First'.
Re^6: Declaring with my, assigning, and testing in same line (attributes)
by LanX (Sage) on Dec 13, 2015 at 19:39 UTC
    Interesting...

    I had the model for CODE attributes in mind which happen at compile time:

    (excerpt from attributes )

    3. Code: package X; sub foo : lvalue ; Effect: use attributes X => \&foo, "lvalue";

    That's why Attribute::Handlers is putting such emphasis onto the compilation phase when the association happens.

    but LEXICAL attributes seem to call import with each declaration at run-time (which is consistent with the fact that my has a compile and a run-time effect.)

    2. Code: package Felis; my $cat : Nervous; Effect: use attributes (); attributes::->import(Felis => \$cat, "Nervous");

    maybe Attribute::Handlers should be clearer about this?

    > There are ways how not to be wrong.

    (A disturbance in the Force happens)

    Now more potential in using attributes I see ... ;-)

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

    update

    ) to be more specific ... this behaviour means that the untie happens almost immediately after the tie, i.e. the potential for possible ugly side effects is practically zero.

      this behaviour means that the untie happens almost immediately after the tie, i.e. the potential for possible ugly side effects is practically zero.
      My benchmarks show attributes are much slower than a (&;@) sub, especially for short lists (many thousand times).
      ($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,
        > are much slower

        Did you expect otherwise? :)

        That's normally the prize for syntactic sugar...

        I wouldn't be surprised if Attribute::Handlers is to be blamed for a big part of the delay.

        Using attributes directly might speed up things, but it will still be considerably slower.

        But when used in the context of debugging or similar speed wouldn't matter that much.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1150166]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2023-03-22 13:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (60 votes). Check out past polls.

    Notices?