with joplin terminal
you can do:
joplin --profile ~/.config/joplin-desktop mknote test_attachment
add an attachment by
joplin --profile ~/.config/joplin-desktop attach <note> <file>
to spot the note id we do
joplin --profile ~/.config/joplin-desktop ls -l -t n -n 1 -f json
explanation :
ls -l
like the command line 
-t n
means you just want to list "note" (vs task with -t t )
-n 1
means you want the 1irst one only
-f json
-f is the output format, here json
the main pro of -f json is to be able to grab the stdout easily and avoid to use some |cut -d ''
here is my final output
joplin --profile ~/.config/joplin-desktop ls -t n -n 1 -s created_time -f json
[{"id":"7c46776072704f679a73d3e5eeeb4e14","title":"test_attachment","body":"","is_todo":0,"todo_completed":0,"parent_id":"e424345758da42e0be3d7a7574101a4e","updated_time":1536861769606,"user_updated_time":1536861769606,"user_created_time":1536861499476,"encryption_applied":0,"type_":1}]
then I can use attach
joplin --profile ~/.config/joplin-desktop attach 7c467 ~/Images/P_20180902_184400.jpg
you could notice that I did not provide the complet note ID because joplin ls -l display the first 4 car so you could use also 4 car or more
now
joplin --profile ~/.config/joplin-desktop ls -t n -n 1 -s created_time -f json
[{"id":"7c46776072704f679a73d3e5eeeb4e14","title":"test_attachment","body":"\n\n","is_todo":0,"todo_completed":0,"parent_id":"e424345758da42e0be3d7a7574101a4e","updated_time":1536861769606,"user_updated_time":1536861769606,"user_created_time":1536861499476,"encryption_applied":0,"type_":1}]
The image will be added at the end of the note.
Now, If you plan to deal with the image position, I wont export anything from the database into markdown file. Instead I will read the text database and wrap the joplin terminal to build the notes the way I want.
For example
In your perl script:
- you could split the text body in 3 pieces for example, to put the image after each piece of body
- run joplin mknote title1
- grab the note ID with
joplin --profile ~/.config/joplin-desktop ls -t n -n 1 -s created_time -f json
- joplin set <noteid_found_in_point_3> body body_piece1
- joplin attach <noteid_found_in_point_3> /path/to/image1.jpg
- repeat 4 and 5 for body_piece2 and body_piece3 and each attachment
ps : i'm not sure of the result with
joplin set <noteid_found_in_point_3> body body_piece1
It's possible that with
joplin set <noteid_found_in_point_3> body body_piece2
the 2nd piece override the complet body.
In that case, to concatenate the content of the existing body, redo
joplin --profile ~/.config/joplin-desktop ls -t n -n 1 -s created_time -f json
to grab the "body" and then you could update the body withtout risk