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