O. Olson <> wrote:
> I am for most part familiar C++/Java programming - but not much with
> Perl. I am trying to figure out if I can use C style structures in
> Perl - they are not classes per se - but sort of similar.
There's the one problem that Perl is neither C++ (or C) nor Java and
it doesn't have C style structures...
> In the following code - which is part of a larger program - I tried
> to group the hours, minutes and seconds of time into a combined data
> structure. However once I set the current time ($curr_time) - the next
> midnight time ($next_midnight) gets changed. Can anyone guess why??
> As I said I am not much into Perl - so I might be doing something
> obviously wrong.
> -------------------------------------------------------------------
> use warnings;
> use strict;
> # Declaration of a data structure that would hold a single line or
> record of the file
> # This would ensure that the file would never remain open if the
> program exits prematurely
> my $time = {
> HOUR => my $hour,
> MINUTE => my $minute,
> SECOND => my $second
> };
> # Defining two variables of the type time
> my $curr_time = $time; #Contains the hour, minute, second
> my $next_midnight = $time; #Contains the hour, minute, second
> # Setting the Next Midnight Time
> ($next_midnight->{HOUR}, $next_midnight->{MINUTE}, $next_midnight-
> >{SECOND}) = (23, 59, 5
;
> print "Next Midnight Time: "; &printTime($next_midnight); print "\n";
> # Setting the current time
> ($curr_time->{SECOND}, $curr_time->{MINUTE}, $curr_time->{HOUR}) =
> localtime();
> print "curr_time Time: "; &printTime($curr_time); print "\n";
> print "But now, Next Midnight Time: "; &printTime($next_midnight);
> print "\n";
> # This function would print the time variable passed on to it, without
> adding a new line character to it
> sub printTime()
> {
> # Declaring a temporary time variable
> my $temp_time = $time;
> $temp_time = $_[0];
> print "$temp_time->{HOUR}:$temp_time->{MINUTE}:$temp_time-
> >{SECOND}";
> }
> ------------------------------------------------------------
> Console Output:
> Next Midnight Time: 23:59:58
> curr_time Time: 22:50:12
> But now, Next Midnight Time: 22:50:12
> ---------------------------------------------------------
> Why did "Next Midnight Time" change?
I can't answer this queston - my question is how you did get that
output at all since (for several reasons) your above code isn't
valid Perl?
I guess you need to free yourself at least a bit of your C++/Java
background and use instead what Perl gives you. While there are
many ways to do it, my first approach would probably be to create a
package for times like the following (with a bit of code at the start
that uses the package and outputs something similar to what you
posted):
------8<-------------------------------------------------------#!/usr/bin/perl
#!/usr/bin/perl
use strict;
use warnings;
my $current_time = new MyTime( );
my $next_midnight = new $current_time( 23, 59, 58 );
print "Next Midnight Time: " . $next_midnight->as_text . "\n" .
"curr_time Time: " . $current_time->as_text . "\n",
"But now, Next Midnight Time: " . $next_midnight->as_text . "\n";
package MyTime;
=pod
=head1 METHODS
=over 4
=item new()
new() can be called with 0 to 3 arguments: if there is none the new
object is initialized to the current (local) time, otherwise the
first argument is taken to be the hour, the second to be the minute
and the third as the second. Missing arguments are assumed to be 0.
=cut
sub new {
my $inv = shift;
my $class = ref( $inv ) || $inv;
die "Too many arguments\n" unless @_ <=3;
my $self;
if ( @_ == 0 ) { # no arguments
my @time = localtime( );
$self = { hour => $time[ 2 ],
minute => $time[ 1 ],
second => $time[ 0 ] };
} else {
$self = { hour => $_[ 0 ],
minute => $_[ 1 ] || 0,
second => $_[ 2 ] || 0 };
}
return bless $self, $class;
}
=pod
=item as_text()
Returns a string of the time the object represents in "hh:mm:ss" format.
=cut
sub as_text {
my $self = shift;
return sprintf "%02d:%02d:%02d",
$self->{ hour }, $self->{ minute }, $self->{ second };
}
1;
------8<-------------------------------------------------------
That way you can create new objects (similar to C++ or Java)
representing times and print them. Adding methods that return
or change the hour, minute or second is simple, e.g. for getting
or setting the hour you would just need e.g.
sub hour {
my $self = shift;
$self->{ hour } = shift if exists $_[ 0 ];
return $self->{ hour };
}
Of course, this MyTime package would need quite a bit of error
checking (what happens when you pass it non-numeric or negative
arguments or hours above 23 or minutes or seconds above 59?)
but I hope it gives you an idea of how you could do it in Perl.
Regards, Jens
--
\ Jens Thoms Toerring ___
\__________________________
http://toerring.de