fireartist has asked for the wisdom of the Perl Monks concerning the following question:

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... } }

Replies are listed 'Best First'.
Re: OO reference problem
by chromatic (Archbishop) on May 09, 2002 at 16:19 UTC
    I don't see any reason you need the double quotes. Get rid of them, and my quick look at your code makes me think it'll just work right.
Re: OO reference problem
by japhy (Canon) on May 09, 2002 at 17:09 UTC
    As chromatic said, you have double-quote-itis. There's no need to quote a variable all by itself when passing it to a function (or printing it, or assigning its value to something else), unless there's deep magic involved.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: OO reference problem
by fireartist (Chaplain) on May 10, 2002 at 10:55 UTC
    Thanks, both chromatic and japhy,

    yeah, I still have some to learn on when to use quoting and when not to...

    I've fixed a couple of silly mistakes, and documented it, so I'll post the code below, just in case anyone's interested in using / abusing it.

    This is my first attempt at OO perl, after reading the docs yesterday, and I'm very impressed at it's abilities.
    I've written this little package with the intention of cleaning up a CGI script I'm working on, and can already see so many possibilities in expanding this package alone, as well as other areas of my script.
    Thanks for the help, guys.
    #!/usr/bin/perl -wT use strict; my $d = data->new; print $d->hour($d->now('h')), ":"; print $d->minute($d->now('m')), ":"; print $d->second($d->now('s')), " - "; print $d->day($d->now('D')), " day of "; print $d->month($d->now('M')), " "; print $d->year($d->now('Y')), "\n";; { package data; ######################### #Call a new package with # my $d = data->new; # #Grab the localtime #(of when a new package was initialized) #with # $d->now('s'); #seconds # $d->now('m'); #minutes # $d->now('h'); #hour # $d->now('D'); #day # $d->now('M'); #month # $d->now('Y'); #year # #"Nicely" format with # my $value = 1; # $d->day($value); #returns "1st" # $d->month($value); #returns "January" ########################## sub new { my $proto = shift; my $class = ref($proto) || $proto; my @now = localtime(); $now[0] += 1; #format the seconds $now[1] += 1; #format the minutes $now[2] += 1; #format the hour $now[4] += 1; #format the month $now[5] += 1900; #format the year 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 $type = shift; if ($type eq 's') { return $self->{now_second}; } elsif ($type eq 'm') { return $self->{now_minute}; } elsif ($type eq 'h') { return $self->{now_hour}; } elsif ($type eq 'D') { return $self->{now_day}; } elsif ($type eq 'M') { return $self->{now_month}; } elsif ($type eq 'Y') { return $self->{now_year}; } } sub second { my $self = shift; my $value = shift; return $value; } sub minute { my $self = shift; my $value = shift; return $value; } sub hour { my $self = shift; my $value = shift; #line 69 return $value; } 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"; } else { "invalid day" } } sub month { my $self = shift; my $value = shift; if ($value == 1) { "January" } elsif ($value == 2) { "February" } elsif ($value == 3) { "March" } elsif ($value == 4) { "April" } elsif ($value == 5) { "May" } elsif ($value == 6) { "June" } elsif ($value == 7) { "July" } elsif ($value == 8) { "August" } elsif ($value == 9) { "September" } elsif ($value == 10) { "October" } elsif ($value == 11) { "November" } elsif ($value == 12) { "December" } else { "invalid month" } } sub year { my $self = shift; my $value = shift; return $value; } }