blob: a4df06a9b99f3fcc0a88c859873d6254b78a0e6f [file] [log] [blame]
Bram Moolenaarb1c91982018-05-17 17:04:55 +02001*if_perl.txt* For Vim version 8.1. Last change: 2017 Nov 24
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3
4 VIM REFERENCE MANUAL by Sven Verdoolaege
5 and Matt Gerassimof
6
7Perl and Vim *perl* *Perl*
8
91. Editing Perl files |perl-editing|
Bram Moolenaar6aa8cea2017-06-05 14:44:35 +0200102. Compiling Vim with Perl interface |perl-compiling|
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113. Using the Perl interface |perl-using|
Bram Moolenaara5792f52005-11-23 21:25:05 +0000124. Dynamic loading |perl-dynamic|
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14{Vi does not have any of these commands}
15
16The Perl interface only works when Vim was compiled with the |+perl| feature.
17
18==============================================================================
191. Editing Perl files *perl-editing*
20
21Vim syntax highlighting supports Perl and POD files. Vim assumes a file is
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000022Perl code if the filename has a .pl or .pm suffix. Vim also examines the first
Bram Moolenaar071d4272004-06-13 20:20:40 +000023line of a file, regardless of the filename suffix, to check if a file is a
24Perl script (see scripts.vim in Vim's syntax directory). Vim assumes a file
25is POD text if the filename has a .POD suffix.
26
27To use tags with Perl, you need a recent version of Exuberant ctags. Look
28here:
29 http://ctags.sourceforge.net
30
31Alternatively, you can use the Perl script pltags.pl, which is shipped with
32Vim in the $VIMRUNTIME/tools directory. This script has currently more
33features than Exuberant ctags' Perl support.
34
35==============================================================================
Bram Moolenaar6aa8cea2017-06-05 14:44:35 +0200362. Compiling Vim with Perl interface *perl-compiling*
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
38To compile Vim with Perl interface, you need Perl 5.004 (or later). Perl must
39be installed before you compile Vim. Vim's Perl interface does NOT work with
40the 5.003 version that has been officially released! It will probably work
41with Perl 5.003_05 and later.
42
43The Perl patches for Vim were made by:
44 Sven Verdoolaege <skimo@breughel.ufsia.ac.be>
45 Matt Gerassimof
46
Bram Moolenaard2855f52018-07-31 22:23:58 +020047Perl for MS-Windows (and other platforms) can be found at:
48http://www.perl.org/ The ActiveState one should work, Strawberry Perl is a
49good alternative.
Bram Moolenaar071d4272004-06-13 20:20:40 +000050
51==============================================================================
523. Using the Perl interface *perl-using*
53
54 *:perl* *:pe*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000055:pe[rl] {cmd} Execute Perl command {cmd}. The current package
Bram Moolenaar9b451252012-08-15 17:43:31 +020056 is "main". Simple example to test if `:perl` is
57 working: >
58 :perl VIM::Msg("Hello")
Bram Moolenaar071d4272004-06-13 20:20:40 +000059
60:pe[rl] << {endpattern}
61{script}
62{endpattern}
63 Execute Perl script {script}.
64 {endpattern} must NOT be preceded by any white space.
65 If {endpattern} is omitted, it defaults to a dot '.'
66 like for the |:append| and |:insert| commands. Using
67 '.' helps when inside a function, because "$i;" looks
68 like the start of an |:insert| command to Vim.
69 This form of the |:perl| command is mainly useful for
70 including perl code in vim scripts.
71 Note: This command doesn't work when the Perl feature
72 wasn't compiled in. To avoid errors, see
73 |script-here|.
74
75
76Example vim script: >
77
78 function! WhitePearl()
79 perl << EOF
80 VIM::Msg("pearls are nice for necklaces");
81 VIM::Msg("rubys for rings");
82 VIM::Msg("pythons for bags");
83 VIM::Msg("tcls????");
84 EOF
85 endfunction
86<
Bram Moolenaarabd468e2016-09-08 22:22:43 +020087To see what version of Perl you have: >
88 :perl print $^V
89<
Bram Moolenaar071d4272004-06-13 20:20:40 +000090
91 *:perldo* *:perld*
92:[range]perld[o] {cmd} Execute Perl command {cmd} for each line in the
93 [range], with $_ being set to the text of each line in
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000094 turn, without a trailing <EOL>. Setting $_ will change
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 the text, but note that it is not possible to add or
96 delete lines using this command.
97 The default for [range] is the whole file: "1,$".
98
99Here are some things you can try: >
100
101 :perl $a=1
102 :perldo $_ = reverse($_);1
103 :perl VIM::Msg("hello")
104 :perl $line = $curbuf->Get(42)
105<
106 *E299*
107Executing Perl commands in the |sandbox| is limited. ":perldo" will not be
108possible at all. ":perl" will be evaluated in the Safe environment, if
109possible.
110
111
112 *perl-overview*
113Here is an overview of the functions that are available to Perl: >
114
115 :perl VIM::Msg("Text") # displays a message
Bram Moolenaar2f058492017-11-30 20:27:52 +0100116 :perl VIM::Msg("Wrong!", "ErrorMsg") # displays an error message
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 :perl VIM::Msg("remark", "Comment") # displays a highlighted message
118 :perl VIM::SetOption("ai") # sets a vim option
119 :perl $nbuf = VIM::Buffers() # returns the number of buffers
120 :perl @buflist = VIM::Buffers() # returns array of all buffers
121 :perl $mybuf = (VIM::Buffers('qq.c'))[0] # returns buffer object for 'qq.c'
122 :perl @winlist = VIM::Windows() # returns array of all windows
123 :perl $nwin = VIM::Windows() # returns the number of windows
124 :perl ($success, $v) = VIM::Eval('&path') # $v: option 'path', $success: 1
125 :perl ($success, $v) = VIM::Eval('&xyz') # $v: '' and $success: 0
126 :perl $v = VIM::Eval('expand("<cfile>")') # expands <cfile>
127 :perl $curwin->SetHeight(10) # sets the window height
128 :perl @pos = $curwin->Cursor() # returns (row, col) array
129 :perl @pos = (10, 10)
130 :perl $curwin->Cursor(@pos) # sets cursor to @pos
131 :perl $curwin->Cursor(10,10) # sets cursor to row 10 col 10
132 :perl $mybuf = $curwin->Buffer() # returns the buffer object for window
133 :perl $curbuf->Name() # returns buffer name
134 :perl $curbuf->Number() # returns buffer number
135 :perl $curbuf->Count() # returns the number of lines
136 :perl $l = $curbuf->Get(10) # returns line 10
137 :perl @l = $curbuf->Get(1 .. 5) # returns lines 1 through 5
138 :perl $curbuf->Delete(10) # deletes line 10
139 :perl $curbuf->Delete(10, 20) # delete lines 10 through 20
140 :perl $curbuf->Append(10, "Line") # appends a line
141 :perl $curbuf->Append(10, "Line1", "Line2", "Line3") # appends 3 lines
142 :perl @l = ("L1", "L2", "L3")
143 :perl $curbuf->Append(10, @l) # appends L1, L2 and L3
144 :perl $curbuf->Set(10, "Line") # replaces line 10
145 :perl $curbuf->Set(10, "Line1", "Line2") # replaces lines 10 and 11
146 :perl $curbuf->Set(10, @l) # replaces 3 lines
147<
148 *perl-Msg*
149VIM::Msg({msg}, {group}?)
150 Displays the message {msg}. The optional {group}
151 argument specifies a highlight group for Vim to use
152 for the message.
153
154 *perl-SetOption*
155VIM::SetOption({arg}) Sets a vim option. {arg} can be any argument that the
156 ":set" command accepts. Note that this means that no
157 spaces are allowed in the argument! See |:set|.
158
159 *perl-Buffers*
160VIM::Buffers([{bn}...]) With no arguments, returns a list of all the buffers
161 in an array context or returns the number of buffers
162 in a scalar context. For a list of buffer names or
163 numbers {bn}, returns a list of the buffers matching
164 {bn}, using the same rules as Vim's internal
165 |bufname()| function.
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000166 WARNING: the list becomes invalid when |:bwipe| is
167 used. Using it anyway may crash Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168
169 *perl-Windows*
170VIM::Windows([{wn}...]) With no arguments, returns a list of all the windows
171 in an array context or returns the number of windows
172 in a scalar context. For a list of window numbers
173 {wn}, returns a list of the windows with those
174 numbers.
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000175 WARNING: the list becomes invalid when a window is
176 closed. Using it anyway may crash Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177
178 *perl-DoCommand*
179VIM::DoCommand({cmd}) Executes Ex command {cmd}.
180
181 *perl-Eval*
Bram Moolenaar97d62492012-11-15 21:28:22 +0100182VIM::Eval({expr}) Evaluates {expr} and returns (success, value) in list
183 context or just value in scalar context.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 success=1 indicates that val contains the value of
185 {expr}; success=0 indicates a failure to evaluate
186 the expression. '@x' returns the contents of register
187 x, '&x' returns the value of option x, 'x' returns the
188 value of internal |variables| x, and '$x' is equivalent
189 to perl's $ENV{x}. All |functions| accessible from
190 the command-line are valid for {expr}.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000191 A |List| is turned into a string by joining the items
192 and inserting line breaks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
194 *perl-SetHeight*
195Window->SetHeight({height})
196 Sets the Window height to {height}, within screen
197 limits.
198
199 *perl-GetCursor*
200Window->Cursor({row}?, {col}?)
201 With no arguments, returns a (row, col) array for the
202 current cursor position in the Window. With {row} and
203 {col} arguments, sets the Window's cursor position to
204 {row} and {col}. Note that {col} is numbered from 0,
205 Perl-fashion, and thus is one less than the value in
206 Vim's ruler.
207
208Window->Buffer() *perl-Buffer*
209 Returns the Buffer object corresponding to the given
210 Window.
211
212 *perl-Name*
213Buffer->Name() Returns the filename for the Buffer.
214
215 *perl-Number*
216Buffer->Number() Returns the number of the Buffer.
217
218 *perl-Count*
219Buffer->Count() Returns the number of lines in the Buffer.
220
221 *perl-Get*
222Buffer->Get({lnum}, {lnum}?, ...)
223 Returns a text string of line {lnum} in the Buffer
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000224 for each {lnum} specified. An array can be passed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 with a list of {lnum}'s specified.
226
227 *perl-Delete*
228Buffer->Delete({lnum}, {lnum}?)
229 Deletes line {lnum} in the Buffer. With the second
230 {lnum}, deletes the range of lines from the first
231 {lnum} to the second {lnum}.
232
233 *perl-Append*
234Buffer->Append({lnum}, {line}, {line}?, ...)
235 Appends each {line} string after Buffer line {lnum}.
236 The list of {line}s can be an array.
237
238 *perl-Set*
239Buffer->Set({lnum}, {line}, {line}?, ...)
240 Replaces one or more Buffer lines with specified
241 {lines}s, starting at Buffer line {lnum}. The list of
242 {line}s can be an array. If the arguments are
243 invalid, replacement does not occur.
244
245$main::curwin
246 The current window object.
247
248$main::curbuf
249 The current buffer object.
250
251
252 *script-here*
253When using a script language in-line, you might want to skip this when the
254language isn't supported. But this mechanism doesn't work: >
255 if has('perl')
256 perl << EOF
257 this will NOT work!
258 EOF
259 endif
260Instead, put the Perl/Python/Ruby/etc. command in a function and call that
261function: >
262 if has('perl')
263 function DefPerl()
264 perl << EOF
265 this works
266 EOF
267 endfunction
268 call DefPerl()
269 endif
270Note that "EOF" must be at the start of the line.
271
Bram Moolenaara5792f52005-11-23 21:25:05 +0000272==============================================================================
2734. Dynamic loading *perl-dynamic*
274
Bram Moolenaare06c1882010-07-21 22:05:20 +0200275On MS-Windows and Unix the Perl library can be loaded dynamically. The
276|:version| output then includes |+perl/dyn|.
Bram Moolenaara5792f52005-11-23 21:25:05 +0000277
Bram Moolenaare06c1882010-07-21 22:05:20 +0200278This means that Vim will search for the Perl DLL or shared library file only
279when needed. When you don't use the Perl interface you don't need it, thus
280you can use Vim without this file.
281
282
283MS-Windows ~
Bram Moolenaara5792f52005-11-23 21:25:05 +0000284
Bram Moolenaar69154f22010-07-18 21:42:34 +0200285You can download Perl from http://www.perl.org. The one from ActiveState was
286used for building Vim.
287
288To use the Perl interface the Perl DLL must be in your search path.
289If Vim reports it cannot find the perl512.dll, make sure your $PATH includes
Bram Moolenaarfcb7ab62010-07-20 11:16:17 +0200290the directory where it is located. The Perl installer normally does that.
Bram Moolenaare18c0b32016-03-20 21:08:34 +0100291In a console window type "path" to see what directories are used. The
292'perldll' option can be also used to specify the Perl DLL.
Bram Moolenaara5792f52005-11-23 21:25:05 +0000293
294The name of the DLL must match the Perl version Vim was compiled with.
Bram Moolenaar69154f22010-07-18 21:42:34 +0200295Currently the name is "perl512.dll". That is for Perl 5.12. To know for
Bram Moolenaara5792f52005-11-23 21:25:05 +0000296sure edit "gvim.exe" and search for "perl\d*.dll\c".
297
Bram Moolenaard94464e2015-11-02 15:28:18 +0100298
299Unix ~
300
301The 'perldll' option can be used to specify the Perl shared library file
302instead of DYNAMIC_PERL_DLL file what was specified at compile time. The
303version of the shared library must match the Perl version Vim was compiled
304with.
305
Bram Moolenaara5792f52005-11-23 21:25:05 +0000306==============================================================================
Bram Moolenaar91f84f62018-07-29 15:07:52 +0200307 vim:tw=78:ts=8:noet:ft=help:norl: