Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Schizophrenic var

by eyepopslikeamosquito (Archbishop)
on Dec 06, 2022 at 10:39 UTC ( [id://11148602]=note: print w/replies, xml ) Need Help??


in reply to Re: Schizophrenic var
in thread Schizophrenic var

for sure in your research you read create-your-own-dualvars

Ha ha, this has now become hilarious!

Yesterday in Re^3: Rosetta Code: Long List is Long (Updated Solutions - dualvar), I claimed "Despite using Perl for 20 years, I'd never heard of dualvar before" ... yet on seeing your reference to good old brian_d_foy's dualvar article just now, I rushed to my bookshelf to find brian's superb book still sitting there, along with my customary book notes scribbled on a piece of paper inside the book ... which noted the existence of dualvar on page 30 as "interesting"! Oops, looks like my memory is starting to fail.

Also interesting is that the paper copy had some nice levity to it which is sadly missing from the Web version ... at least the paper copy concludes the dualvar item with "now that you know this, don't start using it everywhere. Save it for very special situations, like impressing people at conferences and cocktail parties." ... or impressing people at Perl Monks in the case of marioroy. :)

Replies are listed 'Best First'.
Re^3: Schizophrenic var
by marioroy (Prior) on Jul 01, 2023 at 05:58 UTC

    I used "dualvar" also, in MCE::Shared::Cache. When max_age is specified during construction of the cache, the dualvar is used internally to store the expiration time with the key name in the keys array.

    # update expiration $keys->[ $off ] = ( $exptime >= 0 ) ? dualvar( time + $exptime, $_[1] ) : dualvar( -1, $_[1] );

    Basically, the "dualvar" capability in Perl allows the "max_age" feature without increasing the memory consumption of the cache or impacting performance.

      Interesting!

      In computer programming, a semipredicate problem occurs when a subroutine intended to return a useful value can fail, but the signalling of failure uses an otherwise valid return value. The problem is that the caller of the subroutine cannot tell what the result means in this case.

      The division operation yields a real number, but fails when the divisor is zero. If we were to write a function that performs division, we might choose to return 0 on this invalid input. However, if the dividend is 0, the result is 0 too. This means there is no number we can return to uniquely signal attempted division by zero, since all real numbers are in the range of division.

      -- from Semipredicate problem (wikipedia)

      Vaguely related is C++-17's std::variant and Haskell's Type Inference system, with types like Maybe Real and Either String Char.

      So perhaps we can say that Perl's dualvar helps solve the Semipredicate problem ... though admittedly, without dualvar, ordinary Perl scalars can use undef to signal invalid input.

      I'm not an expert on any of this, just coincidentally happened to be looking at this topic while updating my notes on exception handling vs error returns in functions. :) Ideas/corrections and other cool references on this topic welcome!

      References Added Later

      • std::error_code (cppreference.com, since C++11) - a platform-dependent error code
      • std::expected (cppreference.com, since C++23) - an object of std::expected<T,E> holds an expected value of type T, or an unexpected value of type E; it's never valueless
      • std::unexpected (cppreference.com, since C++23)
      • Expect the expected (youtube) by Andrei Alexandrescu
      • std::variant (cppreference.com, since C++17) - a type-safe union; you can avoid exceptions by having a function return, for example, a std::variant<string, error_code> (similar in style to Haskell) - see Stroustrup, A Tour of C++, p 209 (superseded by std::expected?)
      • std::optional (cppreference.com, since C++17 - manages an optional contained value, i.e. a value that may or may not be present)

        > Save it for very special situations, like impressing people at conferences and cocktail parties.

        The use of "dualvar" in MCE::Shared::Cache is not due to schizophrenic behavior or trying to impress people. Nothing like that.

        There was a dated article on the web (I misplaced the link) where someone compared several pure-Perl caching modules. In addition to performance, the author of the article compared memory consumption. The thing that I remember is the extra memory consumption using a hash to store the expiration time.

        For me, "dualvar" solved a problem. I was able to add the "max_age" feature to MCE::Shared::Cache without greatly impacting performance or spike in memory consumption.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-16 19:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found