Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

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

by LanX (Saint)
on Dec 13, 2015 at 14:53 UTC ( [id://1150165]=note: print w/replies, xml ) Need Help??


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

> I'm fine with using tie as long as I can untie when I'm done. Otherwise the penalty for this syntactic sugar would be to huge.

At second thought this is a too limited technique... consider

while(1) { my $x :check = some_call(); }

The check would be done only once, so in this case I'd clearly prefer a functional approach

while(1) { check {RULE} my $x = some_call(); }

Using attributes is a pretty way to apply ties, but most use cases I can think of combining this with untie are limited to debugging.

> > > And for completeness , I'm sure you could also use variable :attributes for such checks.

Wrong I was, young padawan. ;-)

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

Replies are listed 'Best First'.
Re^5: Declaring with my, assigning, and testing in same line (attributes)
by choroba (Cardinal) on Dec 13, 2015 at 17:50 UTC
    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,
      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,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2024-03-28 11:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found