Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Following the discussion in Why eval $version? I'd like to propose the following approach.

Usage

use Version::Easy; $VERSION = 'v1.2.3'; # v-str # or... $VERSION = '1.002003'; # float $VERSION = 'v1.2.3_4'; # v-str + alpha $VERSION = '1.002003_4'; # float + alpha

please note that:

  • Version::Easy adds magic to $VERSION
  • it's also declaring $VERSION in the caller's package, so no our needed
  • all formats like floats or v-strings with optional "_alpha" allowed
  • no boilerplate eval or tr/// needed
  • version is used internally, which is the same mechanism applied by use
  • syntax check at runtime
  • toolchain modules with static parser find "$VERSION" as usual
Implementation:
  • $VERSION is a tied scalar,
  • in STORE the version number is checked for correct syntax
  • in FETCH a version object is returned
  • the version object is overloading all necessary operators
  • for older versions of Perl the implementation could adapt dynamically to sane defaults
  • if the performance impact is too heavy for normal runs, it's possible to limit the costly parts only to run inside the test suite.

Doesn't this solve most (or all) problems???

Please comment! Where does this fail? :)

use strict; use warnings; # -------------------------------------------------- package Version::Easy; use Data::Dump qw/pp dd/; use version; sub import { my $pkg = (caller)[0]; my $version; tie $version, 'Version::Easy::Tie'; no strict 'refs'; *{${pkg}."::VERSION"} = \$version; } # -------------------------------------------------- package Version::Easy::Tie; require Tie::Scalar; our @ISA = qw(Tie::StdScalar); use version qw/is_lax/; sub STORE { my ($self,$value)= @_; warn "IN:\t\t",$value; if ( is_lax($value) ) { $$self = version->parse($value); } else { warn "corrupt VERSION format $value"; } } # BUG? perldoc Tie::Scalar says no TIESCALAR needed sub TIESCALAR { my $class = shift; my $version; return bless \$version, $class; } 1; # -------------------------------------------------- # --- fake already required for 'use' demo BEGIN { $INC{'Version/Easy.pm'} = 1; } # -------------------------------------------------- # # DEMO # # package TST; use Version::Easy; # --------- test v-str $VERSION = 'v1.2.3'; warn "Str:\t" ,$VERSION; warn "Float:\t" ,$VERSION->numify; warn "V-str:\t" ,$VERSION->normal; my $v1 = $VERSION; # --------- test float $VERSION = '1.002003'; warn "Str:\t" ,$VERSION; warn "Float:\t" ,$VERSION->numify; warn "V-str:\t" ,$VERSION->normal; my $v2 = $VERSION; warn "Are both Version equal?:", $v1 == $v2; warn "v-str bigger?:", $v1 > 'v1.2.2'; warn "v-str smaller?:", $v1 < 'v1.2.4'; warn "v-str not smaller?:", $v1 < 'v1.2.2'; # # --------------------------------------------------

-*- mode: compilation; default-directory: "d:/exp/" -*- Compilation started at Fri Jul 10 19:13:58 C:/Perl_524/bin\perl.exe d:/exp/Version_Easy.pl IN: v1.2.3 at d:/exp/Version_Easy.pl line 29. Str: v1.2.3 at d:/exp/Version_Easy.pl line 69. Float: 1.002003 at d:/exp/Version_Easy.pl line 70. V-str: v1.2.3 at d:/exp/Version_Easy.pl line 71. IN: 1.002003 at d:/exp/Version_Easy.pl line 29. Str: 1.002003 at d:/exp/Version_Easy.pl line 78. Float: 1.002003 at d:/exp/Version_Easy.pl line 79. V-str: v1.2.3 at d:/exp/Version_Easy.pl line 80. Are both Version equal?:1 at d:/exp/Version_Easy.pl line 84. v-str bigger?:1 at d:/exp/Version_Easy.pl line 86. v-str smaller?:1 at d:/exp/Version_Easy.pl line 87. v-str not smaller?: at d:/exp/Version_Easy.pl line 88. Compilation finished at Fri Jul 10 19:13:58

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery


In reply to RFC: Version::Easy by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-19 20:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found