Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6135085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:26:11+00:00 2026-05-23T17:26:11+00:00

I have a Perl script that’s trying to set some configured DateTime and DateTime::Duration

  • 0

I have a Perl script that’s trying to set some configured DateTime and DateTime::Duration instances as Readonly constants. But I see strange behavior when trying to do math on these objects if they are Readonly. Here’s a minimal example:

#!/usr/bin/perl -w

use strict;
use warnings;

use DateTime;
use Readonly;

Readonly my $X => DateTime->now;
my $x = DateTime->now;

Readonly my $Y => DateTime::Duration->new( days => 3 );
my $y = DateTime::Duration->new( days => 3 );

my $a = $X - $Y;
my $b = $x - $y;

print "$a\n";
print "$b\n";

On my system (Perl 5.10.0 on OSX) this displays:

$ ./datetime_test.pl 
Argument "2011-07-12T20:36:08" isn't numeric in subtraction (-) at ./datetime_test.pl line 15.
-4305941629
2011-07-09T20:36:08

So it looks like making the DateTime and the DateTime::Duration Readonly causes them to function incorrectly. Is this a bug? Or am I using Readonly wrong? I’ve also tried Readonly::Scalar and Readonly::Scalar1, and both behave the same way.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:26:11+00:00Added an answer on May 23, 2026 at 5:26 pm

    The problem is that they’re objects (references), not normal scalars. You would need to Readonly the values contained in the references, not the references themselves; but this turns out to be tricky. Something like this appears to work:

    use Readonly;
    use DateTime;
    
    # you can't just say "Readonly %$dt"; here at least, it dies on blessed refs
    sub makeRO {
      my $dt = shift;
      while (my ($k, $v) = each %$dt) {
        Readonly $dt->{$k} => $v;
      }
    }
    
    my $x = DateTime::Duration->new(days => 3);
    makeRO($x);
    my $y = DateTime::Duration->new(days => 3);
    
    my $a = $x - $y;
    # print "$a\n"; # this isn't overloaded; you'll get "DateTime::Duration=HASH(...)"
    print $a->days, "\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Perl script that I'm attempting to set up using Perl Threads
I have a little Perl script (On Windows) that checks some files for me
I have a perl script that is only keeping the last set of records
I have a perl script that will perform some operations on directories, and I
I have a Perl script that sets up variables near the top for directories
I have a Perl script that I'd like to run on Windows, using either
I have a Perl script that pops up a message box when its work
I have an existing Perl script that uses the FTP object to send a
I have configured Tomcat 6 to serve Perl CGI scripts. That part is well
I have this Perl script with many defined constants of configuration files. For example:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.