if_perl.txt 11 KB

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