Greetings,

I have a package named 'data' (posted below) which is used to initialize some values for my script, and help return 'friendly' data for some quick CGI output.

When I run this code with the 'data' package,
#!/usr/bin/perl -wT use strict; my $d = data->new; print $d->day("10"), "\n"; print $d->now("d"), "\n";
I get this output,
10th 10
What I would like to be able to do, is to get the 'friendly' format of today's date ("10th"), without using a temporary variable.
The only way I can get it so far, is to use
my $temp = $d->now("d"); print $d->day("$temp"), "\n";
I'd like to do this in 1 step though.
something like... print $d->day("$d->now('d')"); (which doesn't work).
{ package data; sub new { my $proto = shift; my $class = ref($proto) || $proto; my @now = localtime(); $now[0] += 1; $now[3] += 1; $now[5] += 1900; my $self = {}; $self->{now_second} = $now[0]; $self->{now_minute} = $now[1]; $self->{now_hour} = $now[2]; $self->{now_day} = $now[3]; $self->{now_month} = $now[4]; $self->{now_year} = $now[5]; bless ($self, $class); return $self; } sub now { my $self = shift; my $value = shift; if ($value eq 's') { return $self->{now_second}; } elsif ($value eq 'm') { return $self->{now_minute}; } elsif ($value eq 'h') { return $self->{now_hour}; } elsif ($value eq 'd') { return $self->{now_day}; } elsif ($value eq 'm') { return $self->{now_month}; } elsif ($value eq 'y') { return $self->{now_year}; } } sub day { my $self = shift; my $value = shift; if ($value == 1) { "1st" } elsif ($value == 2) { "2nd" } elsif ($value == 3) { "3rd" } elsif ($value == 21) { "21st" } elsif ($value == 22) { "22nd" } elsif ($value == 23) { "23rd" } elsif ($value == 31) { "31st" } elsif (($value >= 4) && ($value <= 31)) { return $value . "th"; } } sub month { my $self = shift; my $value = shift; if ($value == 1) { "January" } elsif ($value == 2) { "February" } #etc... } }

In reply to OO reference problem by fireartist

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.