1Devel::REPL::Overview(3U)ser Contributed Perl DocumentatiDoenvel::REPL::Overview(3)
2
3
4
6 Devel::REPL::Overview - overview of Devel::REPL.
7
9 What is a console? How it can assist you?
10 Most modern languages have consoles. The console is an interactive tool
11 that evaluates your input while you type it. It gives you several
12 advantages:
13
14 · Quickly test some thought or tricky expression
15
16 · Run some code bigger than one line without a temporary file
17
18 · Play around with libraries and modules
19
20 · You can even call a console in your script and play around in
21 script's context
22
23 For Ruby it would be irb, for Python is... python by itself and for
24 perl... and there was nothing for perl (except that ugly perl -d -e ""
25 and several failed projects) until Devel::REPL was written by Matt S
26 Trout (a.k.a. mst) from ShadowCatSystems
27 <http://www.shadowcatsystems.co.uk>.
28
29 Devel::REPL - the Perl console
30 REPL stands for Read, Evaluate, Print, Loop. Lets install and try it.
31
32 $ cpan Devel::REPL
33
34 After installation you have a lot of new modules, but the most
35 interesting things are:
36
37 · Devel::REPL
38 A top level module.
39
40 · re.pl
41 Wrapper script, running console.
42
43 And a bunch of plugins (I'll describe them later). In command line
44 type:
45
46 $ re.pl
47
48 If everything is ok you'll see a prompt (underlined $). That's it. You
49 can start typing expressions.
50
51 An example session:
52
53 $ sub factorial {
54
55 > my $number = shift;
56
57 > return $number > 1 ? $number * factorial($number-1) : $number;
58
59 > }
60
61 $ factorial 1 # by the way, comments are allowed
62
63 1 # our return value
64
65 $ factorial 5
66
67 120
68
69 $ [1,2,3,4,5,6,7]
70 $ARRAY1 = [
71 1,
72 2,
73 3, # return values are printed with Data::Dumper::Streamer.
74 4, # See Plugins section
75 5,
76 6,
77 7
78 ];
79
80 $ {apple=>1,fruit=>'apple',cart=>['apple','banana']}
81 $HASH1 = {
82 apple => 1,
83 cart => [
84 'apple',
85 'banana'
86 ],
87 fruit => 'apple'
88 };
89
90 $ package MyPackage; # create a package
91
92 $ sub say_hi { # define a sub
93
94 > print "Hi!\n";
95
96 > } # statement is evaluated only after we've finished typing block.
97 # See Plugins section.
98 > __PACKAGE__
99 MyPackage
100 > package main;
101
102 > __PACKAGE_
103 main
104 > MyPackage->say_hi
105 Hi!
106 1
107 $
108
109 Control files a.k.a. I don't want to type it every time
110 Devel::REPL has a control files feature. Control files are evaluated on
111 session start in the same way as you would type them manually in the
112 console.
113
114 The default control file is located at $HOME/.re.pl/repl.rc.
115
116 You can store there any statements you would normally type in.
117
118 I.e. my $HOME/.re.pl/repl.rc has next lines:
119
120 use feature 'say'; # to don't write \n all the time
121
122 use Data::Dumper;
123
124 # pretty print data structures
125 sub pp { print Data::Dumper->Dump([@_]) }
126
127 You can have multiple control files and they can be anywhere in the
128 file system. To make re.pl use some rc-file other than repl.rc, call it
129 like this:
130
131 $ re.pl --rcfile /path/to/your/rc.file
132
133 If your rc-file is in $HOME/.re.pl directory, you can omit the path:
134
135 $ re.pl --rcfile rc.file
136
137 If you have rc-file with the same name in current directory and you
138 don't want to type path, you can:
139
140 $ re.pl --rcfile ./rc.file
141
142 I want it to bark, fly, jump and swim! or Plugins
143 Plugins extend functionality and change behavior of Devel::REPL.
144 Bundled plugins are:
145
146 · Devel::REPL::Plugin::History
147 No comments. Simply history.
148
149 · Devel::REPL::Plugin::!LexEnv
150 Provides a lexical environment for the Devel::REPL.
151
152 · Devel::REPL::Plugin::DDS
153 Formats return values with Data::Dump::Streamer module.
154
155 · Devel::REPL::Plugin::Packages
156 Keeps track of which package your're in.
157
158 · Devel::REPL::Plugin::Commands
159 Generic command creation plugin using injected functions.
160
161 · Devel::REPL::Plugin::MultiLine::PPI
162 Makes Devel::REPL read your input until your block
163 is finished. What does this means: you can type a part of a block
164 on one line and second part on another:
165
166 $ sub mysub {
167
168 > print "Hello, World!\n"; ## notice prompt change
169
170 > }
171
172 $ mysub
173 Hello, World!
174 1
175 $
176
177 but this *doesn't* mean you can print sub name or identifier
178 on several lines. Don't do that! It won't work.
179
180 There are lots of contributed plugins you can find at CPAN.
181
183 If plugins change and extend functionality of Devel::REPL, profiles are
184 changing your environment (loaded plugins, constants, subs and etc.).
185
186 For example, the Minimal profile, Devel::REPL::Profile::Minimal:
187
188 package Devel::REPL::Profile::Minimal;
189
190 use Moose; ### advanced OOP system for Perl
191
192 ### keep those exports/imports out of our namespace
193 use namespace::autoclean;
194
195 with 'Devel::REPL::Profile'; ## seem perldoc Muse
196
197 sub plugins { ### plugins we want to be loaded
198 qw(History LexEnv DDS Packages Commands MultiLine::PPI);
199 }
200
201 ### the only required sub for profile,
202 ### it is called on profile activation
203 sub apply_profile {
204 my ($self, $repl) = @_;
205 ### $self - no comments, $repl - current instance of Devel::REPL
206
207 $repl->load_plugin($_) for $self->plugins; ### load our plugins
208 }
209
210 1;
211
212 There is also the StandardDevel::REPL::Profile::Standard profile, which
213 contains a number of optional (yet very useful) features.
214
215 To enable some profile use the "--profile" switch:
216
217 $ re.pl --profile SomeProfile
218
219 Alternatively, you can set the environment variable
220 "DEVEL_REPL_PROFILE" to "SomeProfile", or set the "profile" key in your
221 "rcfile" (see Devel::REPL for more information).
222
224perl v5.30.1 2020-01-29 Devel::REPL::Overview(3)