Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Redundant function calls in constructor?

by nemesdani (Friar)
on Apr 02, 2012 at 09:18 UTC ( #962984=perlquestion: print w/replies, xml ) Need Help??
nemesdani has asked for the wisdom of the Perl Monks concerning the following question:

Laudetur, monks. I'm still learning to use OOP in Perl, as I inherited a small bunch of code.
I've got a class, configContainer. In the contructor there is:
$commonConfig_tmp->{ 'DAY' } = ${configContainer->getLocalTime}[3] +; $commonConfig_tmp->{ 'MONTH' } = ${configContainer->getLocalTime}[ +4]; $commonConfig_tmp->{ 'YEAR' } = ${configContainer->getLocalTime}[5 +];
There is a sub getLocalTime, quite self-explanatory:
sub getLocalTime { my @timeData = localtime(time); @timeData[5] += 1900; @timeData[4] += 1; return \@timeData;
Now my question is: in the constructor getLocalTime, thus localtime() is called 3 times. I see this as unnecessary, I could assign the values with 1 call. Or am I missing someting concerning objects / constructors? Thank you for your help.
I'm too lazy to be proud of being impatient.

Replies are listed 'Best First'.
Re: Redundant function calls in constructor?
by GrandFather (Sage) on Apr 02, 2012 at 10:00 UTC

    Assign an array slice to a hash slice:

    @{$commonConfig_tmp}{'DAY', 'MONTH', 'YEAR'} = @{configContainer->get +LocalTime}[3 .. 5];
    True laziness is hard work
Re: Redundant function calls in constructor?
by roboticus (Chancellor) on Apr 02, 2012 at 10:01 UTC

    nemesdani:

    You can use slicing to eliminate the redundant calls:

    $ cat foo.pl use strict; use warnings; use Data::Dump 'dump'; my %h; @h{'A', 'B', 'C'} = @{foo()}[0,1,2]; print dump(\%h); sub foo { [3, 2, 1]; } $ perl foo.pl { A => 3, B => 2, C => 1 }

    The @h{'A', 'B', 'C'} is a hash slice, and the right uses an array slice. You'll find hash and array slicing described in perldoc perldata.

    Update: Fixed link to docs.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Redundant function calls in constructor?
by tinita (Parson) on Apr 02, 2012 at 10:30 UTC
    I could assign the values with 1 call.
    You even should. Theoretically, the first call could happen at the end of a day, month or year. Let's say 2012-01-31 23:59:59. and the next one one second later, 2012-02-01 00:00:00. in which case you'll end up with wrong data (DAY=31, MONTH=02, YEAR=2012). oops
      Indeed. The application is not time-crtical, but why insert a bug when you can avoid it. Thanks!

      I'm too lazy to be proud of being impatient.
        The application is not time-crtical, but why insert a bug when you can avoid it.   [emphasis added]

        <rant>

        The tone of this sentence implies that avoidance of this particular bug is essentially cosmetic and more or less optional.

        It's not just theoretically possible; it will eventually happen that successive calls to  localtime occur on successive days. This will produce impossible dates (e.g., 31 April) or dates that are 'off' by a month or as much as a year (e.g., 31 Dec 2012 vice 31 Dec 2011).

        But if the application can tolerate dates that are impossible or wildly inaccurate, why bother with dates at all?

        </rant>

Re: Redundant function calls in constructor?
by nemesdani (Friar) on Apr 02, 2012 at 10:14 UTC
    Thank you for your answers!

    I'm too lazy to be proud of being impatient.
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://962984]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2018-11-15 14:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My code is most likely broken because:
















    Results (186 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!