blob: a74d88048350114e3e96e0342667db3c628b3796 [file] [log] [blame]
Bram Moolenaar9b451252012-08-15 17:43:31 +02001*if_perl.txt* For Vim version 7.3. Last change: 2012 Aug 02
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|
102. Compiling VIM with Perl interface |perl-compiling|
113. 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==============================================================================
362. Compiling VIM with Perl interface *perl-compiling*
37
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 Moolenaar9b451252012-08-15 17:43:31 +020047Perl for MS-Windows can be found at: http://www.perl.com/
48The ActiveState one should work.
Bram Moolenaar071d4272004-06-13 20:20:40 +000049
50==============================================================================
513. Using the Perl interface *perl-using*
52
53 *:perl* *:pe*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000054:pe[rl] {cmd} Execute Perl command {cmd}. The current package
Bram Moolenaar9b451252012-08-15 17:43:31 +020055 is "main". Simple example to test if `:perl` is
56 working: >
57 :perl VIM::Msg("Hello")
Bram Moolenaar071d4272004-06-13 20:20:40 +000058
59:pe[rl] << {endpattern}
60{script}
61{endpattern}
62 Execute Perl script {script}.
63 {endpattern} must NOT be preceded by any white space.
64 If {endpattern} is omitted, it defaults to a dot '.'
65 like for the |:append| and |:insert| commands. Using
66 '.' helps when inside a function, because "$i;" looks
67 like the start of an |:insert| command to Vim.
68 This form of the |:perl| command is mainly useful for
69 including perl code in vim scripts.
70 Note: This command doesn't work when the Perl feature
71 wasn't compiled in. To avoid errors, see
72 |script-here|.
73
74
75Example vim script: >
76
77 function! WhitePearl()
78 perl << EOF
79 VIM::Msg("pearls are nice for necklaces");
80 VIM::Msg("rubys for rings");
81 VIM::Msg("pythons for bags");
82 VIM::Msg("tcls????");
83 EOF
84 endfunction
85<
86
87 *:perldo* *:perld*
88:[range]perld[o] {cmd} Execute Perl command {cmd} for each line in the
89 [range], with $_ being set to the text of each line in
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000090 turn, without a trailing <EOL>. Setting $_ will change
Bram Moolenaar071d4272004-06-13 20:20:40 +000091 the text, but note that it is not possible to add or
92 delete lines using this command.
93 The default for [range] is the whole file: "1,$".
94
95Here are some things you can try: >
96
97 :perl $a=1
98 :perldo $_ = reverse($_);1
99 :perl VIM::Msg("hello")
100 :perl $line = $curbuf->Get(42)
101<
102 *E299*
103Executing Perl commands in the |sandbox| is limited. ":perldo" will not be
104possible at all. ":perl" will be evaluated in the Safe environment, if
105possible.
106
107
108 *perl-overview*
109Here is an overview of the functions that are available to Perl: >
110
111 :perl VIM::Msg("Text") # displays a message
112 :perl VIM::Msg("Error", "ErrorMsg") # displays an error message
113 :perl VIM::Msg("remark", "Comment") # displays a highlighted message
114 :perl VIM::SetOption("ai") # sets a vim option
115 :perl $nbuf = VIM::Buffers() # returns the number of buffers
116 :perl @buflist = VIM::Buffers() # returns array of all buffers
117 :perl $mybuf = (VIM::Buffers('qq.c'))[0] # returns buffer object for 'qq.c'
118 :perl @winlist = VIM::Windows() # returns array of all windows
119 :perl $nwin = VIM::Windows() # returns the number of windows
120 :perl ($success, $v) = VIM::Eval('&path') # $v: option 'path', $success: 1
121 :perl ($success, $v) = VIM::Eval('&xyz') # $v: '' and $success: 0
122 :perl $v = VIM::Eval('expand("<cfile>")') # expands <cfile>
123 :perl $curwin->SetHeight(10) # sets the window height
124 :perl @pos = $curwin->Cursor() # returns (row, col) array
125 :perl @pos = (10, 10)
126 :perl $curwin->Cursor(@pos) # sets cursor to @pos
127 :perl $curwin->Cursor(10,10) # sets cursor to row 10 col 10
128 :perl $mybuf = $curwin->Buffer() # returns the buffer object for window
129 :perl $curbuf->Name() # returns buffer name
130 :perl $curbuf->Number() # returns buffer number
131 :perl $curbuf->Count() # returns the number of lines
132 :perl $l = $curbuf->Get(10) # returns line 10
133 :perl @l = $curbuf->Get(1 .. 5) # returns lines 1 through 5
134 :perl $curbuf->Delete(10) # deletes line 10
135 :perl $curbuf->Delete(10, 20) # delete lines 10 through 20
136 :perl $curbuf->Append(10, "Line") # appends a line
137 :perl $curbuf->Append(10, "Line1", "Line2", "Line3") # appends 3 lines
138 :perl @l = ("L1", "L2", "L3")
139 :perl $curbuf->Append(10, @l) # appends L1, L2 and L3
140 :perl $curbuf->Set(10, "Line") # replaces line 10
141 :perl $curbuf->Set(10, "Line1", "Line2") # replaces lines 10 and 11
142 :perl $curbuf->Set(10, @l) # replaces 3 lines
143<
144 *perl-Msg*
145VIM::Msg({msg}, {group}?)
146 Displays the message {msg}. The optional {group}
147 argument specifies a highlight group for Vim to use
148 for the message.
149
150 *perl-SetOption*
151VIM::SetOption({arg}) Sets a vim option. {arg} can be any argument that the
152 ":set" command accepts. Note that this means that no
153 spaces are allowed in the argument! See |:set|.
154
155 *perl-Buffers*
156VIM::Buffers([{bn}...]) With no arguments, returns a list of all the buffers
157 in an array context or returns the number of buffers
158 in a scalar context. For a list of buffer names or
159 numbers {bn}, returns a list of the buffers matching
160 {bn}, using the same rules as Vim's internal
161 |bufname()| function.
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000162 WARNING: the list becomes invalid when |:bwipe| is
163 used. Using it anyway may crash Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164
165 *perl-Windows*
166VIM::Windows([{wn}...]) With no arguments, returns a list of all the windows
167 in an array context or returns the number of windows
168 in a scalar context. For a list of window numbers
169 {wn}, returns a list of the windows with those
170 numbers.
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000171 WARNING: the list becomes invalid when a window is
172 closed. Using it anyway may crash Vim.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173
174 *perl-DoCommand*
175VIM::DoCommand({cmd}) Executes Ex command {cmd}.
176
177 *perl-Eval*
178VIM::Eval({expr}) Evaluates {expr} and returns (success, val).
179 success=1 indicates that val contains the value of
180 {expr}; success=0 indicates a failure to evaluate
181 the expression. '@x' returns the contents of register
182 x, '&x' returns the value of option x, 'x' returns the
183 value of internal |variables| x, and '$x' is equivalent
184 to perl's $ENV{x}. All |functions| accessible from
185 the command-line are valid for {expr}.
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000186 A |List| is turned into a string by joining the items
187 and inserting line breaks.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188
189 *perl-SetHeight*
190Window->SetHeight({height})
191 Sets the Window height to {height}, within screen
192 limits.
193
194 *perl-GetCursor*
195Window->Cursor({row}?, {col}?)
196 With no arguments, returns a (row, col) array for the
197 current cursor position in the Window. With {row} and
198 {col} arguments, sets the Window's cursor position to
199 {row} and {col}. Note that {col} is numbered from 0,
200 Perl-fashion, and thus is one less than the value in
201 Vim's ruler.
202
203Window->Buffer() *perl-Buffer*
204 Returns the Buffer object corresponding to the given
205 Window.
206
207 *perl-Name*
208Buffer->Name() Returns the filename for the Buffer.
209
210 *perl-Number*
211Buffer->Number() Returns the number of the Buffer.
212
213 *perl-Count*
214Buffer->Count() Returns the number of lines in the Buffer.
215
216 *perl-Get*
217Buffer->Get({lnum}, {lnum}?, ...)
218 Returns a text string of line {lnum} in the Buffer
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000219 for each {lnum} specified. An array can be passed
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220 with a list of {lnum}'s specified.
221
222 *perl-Delete*
223Buffer->Delete({lnum}, {lnum}?)
224 Deletes line {lnum} in the Buffer. With the second
225 {lnum}, deletes the range of lines from the first
226 {lnum} to the second {lnum}.
227
228 *perl-Append*
229Buffer->Append({lnum}, {line}, {line}?, ...)
230 Appends each {line} string after Buffer line {lnum}.
231 The list of {line}s can be an array.
232
233 *perl-Set*
234Buffer->Set({lnum}, {line}, {line}?, ...)
235 Replaces one or more Buffer lines with specified
236 {lines}s, starting at Buffer line {lnum}. The list of
237 {line}s can be an array. If the arguments are
238 invalid, replacement does not occur.
239
240$main::curwin
241 The current window object.
242
243$main::curbuf
244 The current buffer object.
245
246
247 *script-here*
248When using a script language in-line, you might want to skip this when the
249language isn't supported. But this mechanism doesn't work: >
250 if has('perl')
251 perl << EOF
252 this will NOT work!
253 EOF
254 endif
255Instead, put the Perl/Python/Ruby/etc. command in a function and call that
256function: >
257 if has('perl')
258 function DefPerl()
259 perl << EOF
260 this works
261 EOF
262 endfunction
263 call DefPerl()
264 endif
265Note that "EOF" must be at the start of the line.
266
Bram Moolenaara5792f52005-11-23 21:25:05 +0000267==============================================================================
2684. Dynamic loading *perl-dynamic*
269
Bram Moolenaare06c1882010-07-21 22:05:20 +0200270On MS-Windows and Unix the Perl library can be loaded dynamically. The
271|:version| output then includes |+perl/dyn|.
Bram Moolenaara5792f52005-11-23 21:25:05 +0000272
Bram Moolenaare06c1882010-07-21 22:05:20 +0200273This means that Vim will search for the Perl DLL or shared library file only
274when needed. When you don't use the Perl interface you don't need it, thus
275you can use Vim without this file.
276
277
278MS-Windows ~
Bram Moolenaara5792f52005-11-23 21:25:05 +0000279
Bram Moolenaar69154f22010-07-18 21:42:34 +0200280You can download Perl from http://www.perl.org. The one from ActiveState was
281used for building Vim.
282
283To use the Perl interface the Perl DLL must be in your search path.
284If Vim reports it cannot find the perl512.dll, make sure your $PATH includes
Bram Moolenaarfcb7ab62010-07-20 11:16:17 +0200285the directory where it is located. The Perl installer normally does that.
Bram Moolenaar69154f22010-07-18 21:42:34 +0200286In a console window type "path" to see what directories are used.
Bram Moolenaara5792f52005-11-23 21:25:05 +0000287
288The name of the DLL must match the Perl version Vim was compiled with.
Bram Moolenaar69154f22010-07-18 21:42:34 +0200289Currently the name is "perl512.dll". That is for Perl 5.12. To know for
Bram Moolenaara5792f52005-11-23 21:25:05 +0000290sure edit "gvim.exe" and search for "perl\d*.dll\c".
291
292==============================================================================
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 vim:tw=78:ts=8:ft=help:norl: