Hirohito Higashi | fbe4a8f | 2025-04-27 15:28:30 +0200 | [diff] [blame] | 1 | *if_perl.txt* For Vim version 9.1. Last change: 2025 Apr 27 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Sven Verdoolaege |
| 5 | and Matt Gerassimof |
| 6 | |
| 7 | Perl and Vim *perl* *Perl* |
| 8 | |
| 9 | 1. Editing Perl files |perl-editing| |
Bram Moolenaar | 6aa8cea | 2017-06-05 14:44:35 +0200 | [diff] [blame] | 10 | 2. Compiling Vim with Perl interface |perl-compiling| |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 11 | 3. Using the Perl interface |perl-using| |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 12 | 4. Dynamic loading |perl-dynamic| |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 13 | |
Bram Moolenaar | 25c9c68 | 2019-05-05 18:13:34 +0200 | [diff] [blame] | 14 | {only available when Vim was compiled with the |+perl| feature} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 15 | |
| 16 | ============================================================================== |
| 17 | 1. Editing Perl files *perl-editing* |
| 18 | |
| 19 | Vim syntax highlighting supports Perl and POD files. Vim assumes a file is |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 20 | Perl code if the filename has a .pl or .pm suffix. Vim also examines the first |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 21 | line of a file, regardless of the filename suffix, to check if a file is a |
| 22 | Perl script (see scripts.vim in Vim's syntax directory). Vim assumes a file |
| 23 | is POD text if the filename has a .POD suffix. |
| 24 | |
Bram Moolenaar | 47c532e | 2022-03-19 15:18:53 +0000 | [diff] [blame] | 25 | To use tags with Perl, you need Universal/Exuberant Ctags. Look here: |
| 26 | Universal Ctags (preferred): https://ctags.io |
| 27 | Exuberant Ctags: http://ctags.sourceforge.net |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 28 | |
| 29 | Alternatively, you can use the Perl script pltags.pl, which is shipped with |
| 30 | Vim in the $VIMRUNTIME/tools directory. This script has currently more |
| 31 | features than Exuberant ctags' Perl support. |
| 32 | |
| 33 | ============================================================================== |
Bram Moolenaar | 6aa8cea | 2017-06-05 14:44:35 +0200 | [diff] [blame] | 34 | 2. Compiling Vim with Perl interface *perl-compiling* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 35 | |
| 36 | To compile Vim with Perl interface, you need Perl 5.004 (or later). Perl must |
| 37 | be installed before you compile Vim. Vim's Perl interface does NOT work with |
| 38 | the 5.003 version that has been officially released! It will probably work |
| 39 | with Perl 5.003_05 and later. |
| 40 | |
| 41 | The Perl patches for Vim were made by: |
| 42 | Sven Verdoolaege <skimo@breughel.ufsia.ac.be> |
| 43 | Matt Gerassimof |
| 44 | |
Bram Moolenaar | d2855f5 | 2018-07-31 22:23:58 +0200 | [diff] [blame] | 45 | Perl for MS-Windows (and other platforms) can be found at: |
Bram Moolenaar | f0d58ef | 2018-11-16 16:13:44 +0100 | [diff] [blame] | 46 | http://www.perl.org/ |
| 47 | The ActiveState one should work, Strawberry Perl is a good alternative. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 48 | |
| 49 | ============================================================================== |
| 50 | 3. Using the Perl interface *perl-using* |
| 51 | |
| 52 | *:perl* *:pe* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 53 | :pe[rl] {cmd} Execute Perl command {cmd}. The current package |
Bram Moolenaar | 9b45125 | 2012-08-15 17:43:31 +0200 | [diff] [blame] | 54 | is "main". Simple example to test if `:perl` is |
| 55 | working: > |
| 56 | :perl VIM::Msg("Hello") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 57 | |
Bram Moolenaar | 6c2b7b8 | 2020-04-14 20:15:49 +0200 | [diff] [blame] | 58 | :pe[rl] << [trim] [{endmarker}] |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 59 | {script} |
Bram Moolenaar | 2e693a8 | 2019-10-16 22:35:02 +0200 | [diff] [blame] | 60 | {endmarker} |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 61 | Execute Perl script {script}. |
Bram Moolenaar | 2e693a8 | 2019-10-16 22:35:02 +0200 | [diff] [blame] | 62 | The {endmarker} after {script} must NOT be preceded by |
| 63 | any white space. |
Bram Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 64 | |
Bram Moolenaar | 2e693a8 | 2019-10-16 22:35:02 +0200 | [diff] [blame] | 65 | If [endmarker] is omitted, it defaults to a dot '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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. |
Bram Moolenaar | 5477506 | 2019-07-31 21:07:14 +0200 | [diff] [blame] | 69 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 70 | This form of the |:perl| command is mainly useful for |
| 71 | including perl code in vim scripts. |
| 72 | Note: This command doesn't work when the Perl feature |
| 73 | wasn't compiled in. To avoid errors, see |
| 74 | |script-here|. |
| 75 | |
| 76 | |
Hirohito Higashi | fbe4a8f | 2025-04-27 15:28:30 +0200 | [diff] [blame] | 77 | Example Vim script: > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 78 | |
| 79 | function! WhitePearl() |
| 80 | perl << EOF |
| 81 | VIM::Msg("pearls are nice for necklaces"); |
| 82 | VIM::Msg("rubys for rings"); |
| 83 | VIM::Msg("pythons for bags"); |
| 84 | VIM::Msg("tcls????"); |
| 85 | EOF |
| 86 | endfunction |
| 87 | < |
Bram Moolenaar | abd468e | 2016-09-08 22:22:43 +0200 | [diff] [blame] | 88 | To see what version of Perl you have: > |
| 89 | :perl print $^V |
| 90 | < |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 91 | |
| 92 | *:perldo* *:perld* |
| 93 | :[range]perld[o] {cmd} Execute Perl command {cmd} for each line in the |
| 94 | [range], with $_ being set to the text of each line in |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 95 | turn, without a trailing <EOL>. Setting $_ will change |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 96 | the text, but note that it is not possible to add or |
| 97 | delete lines using this command. |
| 98 | The default for [range] is the whole file: "1,$". |
| 99 | |
| 100 | Here are some things you can try: > |
| 101 | |
| 102 | :perl $a=1 |
| 103 | :perldo $_ = reverse($_);1 |
| 104 | :perl VIM::Msg("hello") |
| 105 | :perl $line = $curbuf->Get(42) |
Bram Moolenaar | f10911e | 2022-01-29 22:20:48 +0000 | [diff] [blame] | 106 | < *E299* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 107 | Executing Perl commands in the |sandbox| is limited. ":perldo" will not be |
| 108 | possible at all. ":perl" will be evaluated in the Safe environment, if |
| 109 | possible. |
| 110 | |
| 111 | |
| 112 | *perl-overview* |
| 113 | Here is an overview of the functions that are available to Perl: > |
| 114 | |
| 115 | :perl VIM::Msg("Text") # displays a message |
Bram Moolenaar | 2f05849 | 2017-11-30 20:27:52 +0100 | [diff] [blame] | 116 | :perl VIM::Msg("Wrong!", "ErrorMsg") # displays an error message |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 117 | :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* |
| 149 | VIM::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* |
| 155 | VIM::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* |
| 160 | VIM::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 Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 166 | WARNING: the list becomes invalid when |:bwipe| is |
| 167 | used. Using it anyway may crash Vim. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 168 | |
| 169 | *perl-Windows* |
| 170 | VIM::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 Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 175 | WARNING: the list becomes invalid when a window is |
| 176 | closed. Using it anyway may crash Vim. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 177 | |
| 178 | *perl-DoCommand* |
| 179 | VIM::DoCommand({cmd}) Executes Ex command {cmd}. |
| 180 | |
| 181 | *perl-Eval* |
Bram Moolenaar | 664f3cf | 2019-12-07 16:03:51 +0100 | [diff] [blame] | 182 | VIM::Eval({expr}) Evaluates {expr} and returns (success, value) in list |
Bram Moolenaar | 97d6249 | 2012-11-15 21:28:22 +0100 | [diff] [blame] | 183 | context or just value in scalar context. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 184 | 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 Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 191 | A |List| is turned into a string by joining the items |
| 192 | and inserting line breaks. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 193 | |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 194 | *perl-Blob* |
Bram Moolenaar | 314dd79 | 2019-02-03 15:27:20 +0100 | [diff] [blame] | 195 | VIM::Blob({expr}) Return |Blob| literal string 0zXXXX from scalar value. |
Bram Moolenaar | 6e5ea8d | 2019-01-12 22:47:31 +0100 | [diff] [blame] | 196 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 197 | *perl-SetHeight* |
| 198 | Window->SetHeight({height}) |
| 199 | Sets the Window height to {height}, within screen |
| 200 | limits. |
| 201 | |
| 202 | *perl-GetCursor* |
| 203 | Window->Cursor({row}?, {col}?) |
| 204 | With no arguments, returns a (row, col) array for the |
| 205 | current cursor position in the Window. With {row} and |
| 206 | {col} arguments, sets the Window's cursor position to |
| 207 | {row} and {col}. Note that {col} is numbered from 0, |
| 208 | Perl-fashion, and thus is one less than the value in |
| 209 | Vim's ruler. |
| 210 | |
| 211 | Window->Buffer() *perl-Buffer* |
| 212 | Returns the Buffer object corresponding to the given |
| 213 | Window. |
| 214 | |
| 215 | *perl-Name* |
| 216 | Buffer->Name() Returns the filename for the Buffer. |
| 217 | |
| 218 | *perl-Number* |
| 219 | Buffer->Number() Returns the number of the Buffer. |
| 220 | |
| 221 | *perl-Count* |
| 222 | Buffer->Count() Returns the number of lines in the Buffer. |
| 223 | |
| 224 | *perl-Get* |
| 225 | Buffer->Get({lnum}, {lnum}?, ...) |
| 226 | Returns a text string of line {lnum} in the Buffer |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 227 | for each {lnum} specified. An array can be passed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 228 | with a list of {lnum}'s specified. |
| 229 | |
| 230 | *perl-Delete* |
| 231 | Buffer->Delete({lnum}, {lnum}?) |
| 232 | Deletes line {lnum} in the Buffer. With the second |
| 233 | {lnum}, deletes the range of lines from the first |
| 234 | {lnum} to the second {lnum}. |
| 235 | |
| 236 | *perl-Append* |
| 237 | Buffer->Append({lnum}, {line}, {line}?, ...) |
| 238 | Appends each {line} string after Buffer line {lnum}. |
| 239 | The list of {line}s can be an array. |
| 240 | |
| 241 | *perl-Set* |
| 242 | Buffer->Set({lnum}, {line}, {line}?, ...) |
| 243 | Replaces one or more Buffer lines with specified |
| 244 | {lines}s, starting at Buffer line {lnum}. The list of |
| 245 | {line}s can be an array. If the arguments are |
| 246 | invalid, replacement does not occur. |
| 247 | |
| 248 | $main::curwin |
| 249 | The current window object. |
| 250 | |
| 251 | $main::curbuf |
| 252 | The current buffer object. |
| 253 | |
| 254 | |
| 255 | *script-here* |
| 256 | When using a script language in-line, you might want to skip this when the |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 257 | language isn't supported. > |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 258 | if has('perl') |
| 259 | perl << EOF |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 260 | print 'perl works' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 261 | EOF |
| 262 | endif |
Bram Moolenaar | b7398fe | 2023-05-14 18:50:25 +0100 | [diff] [blame] | 263 | Note that "EOF" must be at the start of the line without preceding white |
| 264 | space. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 265 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 266 | ============================================================================== |
| 267 | 4. Dynamic loading *perl-dynamic* |
| 268 | |
Bram Moolenaar | e06c188 | 2010-07-21 22:05:20 +0200 | [diff] [blame] | 269 | On MS-Windows and Unix the Perl library can be loaded dynamically. The |
| 270 | |:version| output then includes |+perl/dyn|. |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 271 | |
Bram Moolenaar | e06c188 | 2010-07-21 22:05:20 +0200 | [diff] [blame] | 272 | This means that Vim will search for the Perl DLL or shared library file only |
| 273 | when needed. When you don't use the Perl interface you don't need it, thus |
| 274 | you can use Vim without this file. |
| 275 | |
| 276 | |
| 277 | MS-Windows ~ |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 278 | |
Bram Moolenaar | 69154f2 | 2010-07-18 21:42:34 +0200 | [diff] [blame] | 279 | You can download Perl from http://www.perl.org. The one from ActiveState was |
| 280 | used for building Vim. |
| 281 | |
| 282 | To use the Perl interface the Perl DLL must be in your search path. |
| 283 | If Vim reports it cannot find the perl512.dll, make sure your $PATH includes |
Bram Moolenaar | fcb7ab6 | 2010-07-20 11:16:17 +0200 | [diff] [blame] | 284 | the directory where it is located. The Perl installer normally does that. |
Bram Moolenaar | e18c0b3 | 2016-03-20 21:08:34 +0100 | [diff] [blame] | 285 | In a console window type "path" to see what directories are used. The |
| 286 | 'perldll' option can be also used to specify the Perl DLL. |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 287 | |
| 288 | The name of the DLL must match the Perl version Vim was compiled with. |
Bram Moolenaar | 69154f2 | 2010-07-18 21:42:34 +0200 | [diff] [blame] | 289 | Currently the name is "perl512.dll". That is for Perl 5.12. To know for |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 290 | sure edit "gvim.exe" and search for "perl\d*.dll\c". |
| 291 | |
Bram Moolenaar | d94464e | 2015-11-02 15:28:18 +0100 | [diff] [blame] | 292 | |
| 293 | Unix ~ |
| 294 | |
| 295 | The 'perldll' option can be used to specify the Perl shared library file |
| 296 | instead of DYNAMIC_PERL_DLL file what was specified at compile time. The |
| 297 | version of the shared library must match the Perl version Vim was compiled |
| 298 | with. |
| 299 | |
Christian Brabandt | 55460da | 2023-08-29 21:31:28 +0200 | [diff] [blame] | 300 | Note: If you are building Perl locally, you have to use a version compiled |
| 301 | with threading support for it for Vim to successfully link against it. You can |
| 302 | use the `-Dusethreads` flags when configuring Perl, and check that a Perl |
| 303 | binary has it enabled by running `perl -V` and verify that `USE_ITHREADS` is |
| 304 | under "Compile-time options". |
| 305 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 306 | ============================================================================== |
Bram Moolenaar | 91f84f6 | 2018-07-29 15:07:52 +0200 | [diff] [blame] | 307 | vim:tw=78:ts=8:noet:ft=help:norl: |