Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re^3: Not quite an OO tutorial

by james2vegas (Chaplain)
on Feb 24, 2011 at 06:37 UTC ( #889918=note: print w/replies, xml ) Need Help??

in reply to Re^2: Not quite an OO tutorial
in thread Not quite an OO tutorial

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

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

    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

      Lady_Aleena, my knowledge is pretty basic still, but for what it's worth I found the information here and page 9 here helpful to understand more about how the subs are working. Maybe this will be helpful to you as well.

Re^4: Not quite an OO tutorial
by Lady_Aleena (Curate) on Feb 26, 2011 at 20:01 UTC

    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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2018-05-24 02:50 GMT
Find Nodes?
    Voting Booth?