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

Re: Not quite an OO tutorial

by gnosti (Hermit)
on Feb 24, 2011 at 05:50 UTC ( #889912=note: print w/replies, xml ) Need Help??

in reply to Not quite an OO tutorial

I am pleased with the combined examples of overload, 'use parent' (as opposed to 'our @ISA =....') and a simple yet interesting application.

I think this is a great, minimalist way to understand OO, compared with a big black-boxish and black-magical Moose.

A functional example of solving the same problem, suggested by lostjimmy, would be valuable for demonstrating a different approach.

Good work GF. btw, I'm impressed by your C.V. Hope any friends/family in Christchurch are okay.

Replies are listed 'Best First'.
Re^2: Not quite an OO tutorial
by Lady_Aleena (Curate) on Feb 24, 2011 at 06:21 UTC

    Since GrandFather wrote the object oriented code above for me, I am more than willing to show how I would write it in functions. I let my eyes glaze over on the overdrive bit, so I don't have a functional way to do that (if there is a way, that is).

    Let's start with the module:

    packack Random::Things; use strict; use warnings; use base 'Exporter'; our @EXPORT_OK = qw(RandDayPart RandLight RandWeather RandTense); sub RandDayPart { my @randdaypart = qw(morning evening afternoon night); return $randdaypart[rand @randdaypart]; } sub RandLight { my @randlight = qw(bright dark gloomy dazzling); return $randlight[rand @randlight]; } sub RandWeather { my @randweather = qw(stormy windy rainy calm); return $randweather[rand @randweather]; } sub RandTense { my @randtense = ('was', 'is', 'will be'); return $randtense[rand @randtense]; } 1;

    Now the script:

    use strict; use warnings; use Random::Things qw(RandDayPart RandLight RandWeather RandTense); my $random_day_part = RandDayPart; my $random_light = RandLight; my $random_weather = RandWeather; my $random_tense = RandTense; print "It $random_tense a $random_light and $random_weather $random_da +y_part."

    It may not be nice and have things like inheritance that the OO version has, but it will do the same thing in a crunch.

    Have a cookie and a very nice day!
    Lady Aleena
      I would skip the duplication and the exporting of functions, something like this:

      package Rand; use strict; use warnings; sub _random { my @values = @_; return $values[rand @values]; } sub day_part { return _random(qw(morning evening afternoon night)); } sub light { return _random(qw(bright dark gloomy dazzling)); } sub weather { return _random(qw(stormy windy rainy calm)); } sub tense { return _random('was', 'is', 'will be'); } 1;

      and the example code would look like this:
      use strict; use warnings; use Rand; my $random_day_part = Rand::day_part; my $random_light = Rand::light; my $random_weather = Rand::weather; my $random_tense = Rand::tense; print "It $random_tense a $random_light and $random_weather $random_da +y_part."

        Until I read your post, james2vegas, I thought that the only want to get a subroutine out of a module without use base 'Exporter'; was to use objects which is why I always use base 'Exporter'. I have not seen subroutines constructed like that before. Would you please tell me what that is called (and maybe where I can find it) so that I can do more reading on it? There is more to non-object oriented programming that I don't know which may be why I am having a hard time grasping object oriented programming in the first place. Maybe the first thing I need to do is to get away from the Exporter without objects before getting into objects. I have been tied to Exporter since Lady Aleena's first working module.

        Have a cookie and a very nice day!
        Lady Aleena

        I found information on the constructs that you used, james2vegas, that confused me in Exporter under "Selecting What To Export."

        1. A leading underscore on a subroutine name (_someSub) is just a convention to denote subroutines that are internal and are to be used only in the module.
        2. ModuleName::someSub is just another way to call a subroutine without having to export it explicitly using our @EXPORT or our @EXPORT_OK.

        These things, while minor, are nice to know for my future writings. I will start adding leading underscores to my internal subroutines, since it is a great visual aid. I might also start using ModuleName::someSub when a module of mine has only one or two subroutines. Thanks for showing me these shortcuts! Cookies! :)

        Have a cookie and a very nice day!
        Lady Aleena
Re^2: Not quite an OO tutorial
by stvn (Monsignor) on Feb 24, 2011 at 15:57 UTC
    I think this is a great, minimalist way to understand OO, compared with a big black-boxish and black-magical Moose.

    I will agree that Moose is very black-boxish, but it is actually very much not black-magical. From the very start Moose has tried to avoid all deep black magic hacks that have plagued so many previous attempts at improving Perl OO.

    In short, black magic is inherently fragile and ill advised, we (Moose core devs) avoid it.

    Of course, I am speaking of Moose, not MooseX::Declare which has TONS of black magic in it.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://889912]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2018-06-18 22:18 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.