http://www.perlmonks.org?node_id=1040562


in reply to Re: vec overflow?
in thread vec overflow?

this workaround doesn't look efficient

It is. It takes an lvalue reference (a scalar value) and passes that to vec. It is marginally less efficient than vec( substr( ... ), ... );, but not so much that you would be able to detect it.

However, substr can only provide an lvalue for byte-sized units; where vec can provide an lvalue for 8 byte units.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^3: vec overflow?
by LanX (Saint) on Jun 25, 2013 at 09:46 UTC
    Ok, I had errors testing it out in the debugger, but turned out that just the automatic Data::Dump couldn't handle lvalues.

    Asa suggestion:

    I think using a foreach alias like shown in the docs for substr is a clearer alternative to lvalue-refs.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      I think using a foreach alias like shown in the docs for substr is a clearer alternative to lvalue-refs.

      Maybe, though I've been aware of LVALUE refs, and using them regularly, for a long time, but I've never seen a use of a for alias used that way until I followed your link to a newer set of docs than I have locally.

      Comparing:

      my $ref = \ substr( $astring, $start, $len ); vec( $$ref, $offset, $size ) = 1;

      To:

      vec( $_, $offset, $size ) = 1 for substr( $astring, $start, $len );

      The first one is very familiar (to me), whereas the latter just looks weird.

      The presence of \ and $$ref seem stronger clues than the $_ and for.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        A matter of taste ...

        for me

        vec( $_, $offset, $size ) = 1 for substr( $astring, $start, $len );

        has no big advantage over

        vec( substr( $astring, $start, $len ), $offset, $size ) = 1;

        but

        for my $chunk ( substr( $astring, $start, $len ) ) { ... vec( $chunk, $offset, $size ) = 1; ... }

        looks better for me than de/referencing and has the advantage to be documented in the docs.

        And your use of \ substr( $astring, $start, $len ); is less frequent and makes me wondering about precedences.

        (The practice to reference lvalues is still somehow new for me...)

        OTOH your approach has the (dis?)advantage that the ref can be passed around w/o being restricted to the body of a for loop...

        Well ... I'll better continue meditating about it after seeing more use cases! :)

        Cheers Rolf

        ( addicted to the Perl Programming Language)