emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
blob 52524c432c1b6b2682c14442ca7ff2b1f6d761db 6243 bytes (raw)
name: org-contrib/babel/languages/ob-doc-perl.org 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
 
#+OPTIONS:    H:3 num:nil toc:2 \n:nil ::t |:t ^:{} -:t f:t *:t tex:t d:(HIDE) tags:not-in-toc
#+STARTUP:    align fold nodlcheck hidestars oddeven lognotestate hideblocks
#+SEQ_TODO:   TODO(t) INPROGRESS(i) WAITING(w@) | DONE(d) CANCELED(c@)
#+TAGS:       Write(w) Update(u) Fix(f) Check(c) noexport(n)
#+TITLE:      Perl in Org Mode
#+AUTHOR:     Daniel M. German
#+EMAIL:      dmg[at]uvic[dot]ca
#+LANGUAGE:   en
#+HTML_HEAD:      <style type="text/css">#outline-container-introduction{ clear:both; }</style>
#+LINK_UP:    ../languages.html
#+LINK_HOME:  https://orgmode.org/worg/
#+EXCLUDE_TAGS: noexport

#+name: banner
#+begin_export html
  <div id="subtitle" style="float: center; text-align: center;">
  <p>
  Org Mode support for <a href="http://www.perl.org/">Perl</a>
  </p>
  <p>
  <a href="http://www.perl.org/">
  <img src="./images/perl5raptor.png"/>
  </a>
  </p>
  </div>
#+end_export

* Template Checklist [11/12]                                       :noexport:
  - [X] Revise #+TITLE:
  - [X] Indicate #+AUTHOR:
  - [X] Add #+EMAIL:
  - [X] Revise banner source block [3/3]
    - [X] Add link to a useful language web site
    - [X] Replace "Language" with language name
    - [X] Find a suitable graphic and use it to link to the language
      web site
  - [X] Write an [[Introduction]]
  - [X] Describe [[Requirements%20and%20Setup][Requirements and Setup]]
  - [X] Replace "Language" with language name in [[Org%20Mode%20Features%20for%20Language%20Source%20Code%20Blocks][Org Mode Features for Language Source Code Blocks]]
  - [X] Describe [[Header%20Arguments][Header Arguments]]
  - [X] Describe support for [[Sessions]]
  - [X] Describe [[Result%20Types][Result Types]]
  - [ ] Describe [[Other]] differences from supported languages
  - [X] Provide brief [[Examples%20of%20Use][Examples of Use]]
* Introduction

This document is a short introduction to using perl within org-mode. 


* Requirements and Setup

The only requirement is installed in the computer where org babel is executing the scripts.


#+begin_src emacs-lisp :exports code
  (org-babel-do-load-languages
   'org-babel-load-languages
   '((lisp . t)))
#+end_src

* Org Mode Features for Perl Code Blocks
** Header Arguments

The support of perl in Babel is basic. There 
There are no language-specific arguments for perl code blocks.

** Result Types

The only supported type is ~value~

** Support for sessions

There is no support for sessions.

** var

It is possible ot pass several variables to perl, including table variables. See below.

* Examples of Use

These are two simple examples:

#+BEGIN_EXAMPLE
#+BEGIN_SRC perl :results value
"hello world";
#+END_SRC

#+RESULTS:
: hello world
#+END_EXAMPLE

#+begin_src perl 
10 * 20 + 5;
#+end_src

#+RESULTS:
: 205
#+end_example

#+RESULTS: countingTo10

* Other types of output

Perl scripts might generate data that is parsed by Org. Unfortunately
its current support is not very powerful. Currently there is only one method to receive data: ~:results value~. This is the default.
The result of the code block (the value of the last expression evaluated) if returned to org. If the result is an array (up to two dimensions)
it is interpreted as a table. Some examples below:


#+BEGIN_EXAMPLE
#+BEGIN_SRC perl :results value
[[1,2],[2,4]]
#+END_SRC

#+RESULTS:
| 1 | 2 |
| 2 | 4 |
#+END_EXAMPLE

#+BEGIN_SRC perl :results value
10 + 20
#+END_SRC

#+RESULTS:
#+begin_example
30
#+end_example

When returning an array, it is important to return a reference to the array. Otherwise it is interpreted as an scalar. 

For example, this returns the size of the array:

#+BEGIN_EXAMPLE
#+BEGIN_SRC perl :results value
my @result ;
$i = 0;
for $j ('a'..'e')  {
   $result[$i] = $j;
   $i ++;
}
@result;
#+END_SRC

#+RESULTS:
: 5
#+END_EXAMPLE
But this returns the values of the array and creates the corresponding table

#+BEGIN_EXAMPLE
#+BEGIN_SRC perl :results value
my @result ;
$i = 0;
for $j ('a'..'e')  {
   $result[$i] = $j;
   $i ++;
}
\@result;
#+END_SRC

#+RESULTS:
| a |
| b |
| c |
| d |
| e |
#+END_EXAMPLE

#+BEGIN_EXAMPLE
#+BEGIN_SRC perl :results value
my @result ;
for $i (0..3) {
   for $j (0..2) {
      $result[$i][$j] = $j*$i+$j;
   }
}
\@result;
#+END_SRC

#+RESULTS:
| 0 | 1 | 2 |
| 0 | 2 | 4 |
| 0 | 3 | 6 |
| 0 | 4 | 8 |
#+END_EXAMPLE

* Using tables as input

The most useful feature of using Perl within org is the ability to use
tables as input to scripts. 

Let us assume we have the following table:

#+BEGIN_EXAMPLE
#+NAME:exampletable
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
#+END_EXAMPLE

We want to use this table as input. Org passes a table to perl as a
reference to an array of anonymous one-dimension arrays. In a nutshell, you can
access an element of a table using ~$$nameTable[row][column]~. Remember,
in perl indexes are zero based: For instance, this block simply returns the input table. Please note that
because data is already a reference we can simply return it.

#+BEGIN_EXAMPLE
#+name: example1usingTable 
#+begin_src perl :var data=exampletable :results table :type value
$data
#+end_src

#+RESULTS: example1usingTable
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
#+END_EXAMPLE

One challenge, however, is to know how big the table is. Perl does not have 
native two dimensional arrays. Instead, it uses arrays of arrays (each sub-array
can have any size). In the block below we use a function (~org_table_size~) to 
return the number of columns and rows in a table.

#+BEGIN_EXAMPLE
#+name: example2usingTable
#+begin_src perl :results value :var data=exampletable 
# first we need to define two functions that will make our life easier
sub org_table_size
{
    # return the number of columns and rows in a table
    my ($table) = @_;
    my $y = $$table[0];
    return (scalar(@$y), scalar (@$table));
}

my @result ;

my ($cols, $rows) = org_table_size($data);

## transpose the input table

for my $i ($0..$cols-1) {
    for my $j (0 .. $rows-1) {
        $result[$i][$j] = $$data[$j][$i];
    }
}
\@result;
#+end_src

#+RESULTS: example2usingTable
| 1 | 2 | 3 | 4 |
| a | b | c | d |
#+END_EXAMPLE



* Links to Tutorials and Other Resources

The best resource for perl is [[https://perldoc.perl.org][The Perl programming documentation]] project.


debug log:

solving 52524c43 ...
found 52524c43 in https://list.orgmode.org/orgmode/CAEBXXD8nbtgMEpGUxRs52RhgXvRxog1ApsQzkPwP0ivLfVJ+BQ@mail.gmail.com/

applying [1/1] https://list.orgmode.org/orgmode/CAEBXXD8nbtgMEpGUxRs52RhgXvRxog1ApsQzkPwP0ivLfVJ+BQ@mail.gmail.com/
diff --git a/org-contrib/babel/languages/ob-doc-perl.org b/org-contrib/babel/languages/ob-doc-perl.org
new file mode 100644
index 00000000..52524c43

1:53: trailing whitespace.
This document is a short introduction to using perl within org-mode. 
1:70: trailing whitespace.
The support of perl in Babel is basic. There 
1:98: trailing whitespace.
#+begin_src perl 
1:135: trailing whitespace.
When returning an array, it is important to return a reference to the array. Otherwise it is interpreted as an scalar. 
1:195: trailing whitespace.
tables as input to scripts. 
Checking patch org-contrib/babel/languages/ob-doc-perl.org...
Applied patch org-contrib/babel/languages/ob-doc-perl.org cleanly.
warning: squelched 5 whitespace errors
warning: 10 lines add whitespace errors.

index at:
100644 52524c432c1b6b2682c14442ca7ff2b1f6d761db	org-contrib/babel/languages/ob-doc-perl.org

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).