'Programming/Perl'에 해당되는 글 2건

  1. 2012.10.05 Chapter 4 & Chapter 5
  2. 2012.10.04 Perl 프로그래밍 기본 셋팅
Programming/Perl2012. 10. 5. 02:44



Chapter 04. 사용자 함수



함수의 이름 ? → & 기호가 붙는다.


사용자 함수를 만드는 법에 대해서 알아보자.


Sub NAME

{

}


Call ? → &NAME; 간단하네

& <-- 이걸 생략할 수 있지만, 만약 함수의 구현이 뒤쪽에 되어 있을 경우, &를 반드시 명시해 주어야 한다.

&를 구분하지 않을 경우 이것이 System Library Func인지 아니면 User Function인지 구분하긴 쉽지 않을듯..


use strict pragma를 쓴다 좋은 프로그래밍 습관을 강제적으로 적용받는다.


my는 scope variable을 지정할 때 쓰면 된다.

사용방법은 my ($what, @array) = @_;

이런식으로~?


subroutine func같은 경우 return값은 가장 마지막 연산..

return을 명시해줘도 되는데 귀찮으닌깐 안쓰는게 최고당 ^^;



Chapter 05. 입력과 출력


Linux의 대표적인 3가지 입출력 STDIN, STDOUT, STDERR


$line = <STDIN>

chomp($line)

보통 표준입력을 이렇게 받지만..

if you feel annoyed


chomp($line = <STDIN>)


Diamond operator... <-- Larry 딸내미가 지어줬다고 함..


command line 부분 argument 처리하기

@ARGV ← 인자를 읽어들인다.

ex ) foreach(@ARGV) print


Array를 Interpolation할 때

if print @array <-- 그냥 한줄씩 출력

print "@array" 한칸 띄어진다. 왜? 그렇게 만들었어요



File을 open할는 open method를 이용

open CONFIG, “<dino”


특수문자의 의미는 아래와 같다.

< : Input

> : Output

>> : If file exists, it will add strings(characters)


Perl 5.6?


open CONFIG, “<”, “dino


die Method? → kill the program with the log from $!

Warn is practically same with Die however, it does not close a program.




Print FileHandle “blablabla~~~”


input task using Diamond operator



./a.pl ab cd def

while(<>)

{

print

}

argv에 있는 것들을 읽어 그것을 출력한다



오늘 공부 끄읕~ ^^


'Programming > Perl' 카테고리의 다른 글

Perl 프로그래밍 기본 셋팅  (0) 2012.10.04
Posted by 박세범
Programming/Perl2012. 10. 4. 15:05

과제를 위해서 현재 Perl을 공부하고 있다.

책은 Learning Perl 5/e 번역본 책인데, 번역이 조금 마음에 안들긴 하지만 책 자체는 매우 훌륭한것 같다.

먼저 Perl을 시작하기 앞서서, Linux 환경이 조금 낯선 나이기 때문에  Vim Editor를 먼저 Setting해 보앗다.


http://skyloader.tistory.com/2


skyloader님의 blog에는 3가지 plug 인을 추천해주셨는데, 아직 vim에 익숙치 않아 엄청난 파워를 느낄순 없지만

Perl 코딩을 하는데 있어서 매우 큰 도움을 줄거라 확신한다.


1. Perl omni completion Plug in

http://www.vim.org/scripts/script.php?script_id=2852


2. perl-support-vim

http://www.vim.org/scripts/script.php?script_id=556


3. textMate-style nippets for vim

http://www.vim.org/scripts/script.php?script_id=2540


.vimrc는 검색하다가 발견한 것으로 대체한다.


http://www.codedanger.com/caglar/archives/409


사용법 및 설치법은 Manual에 자세히 설명되어 있으니 생략하도록 한다.


현재 .vimrc는 아래와 같이 설정되어 있다.


"syntax highlighting

set bg=light

syntax on

 

set ruler

set number

set smarttab

set fileformats=unix,dos,mac " support all three, in this order

set formatoptions=tcqor " t=text, c=comments, q=format with "gq", o,r=autoinsert comment leader

set cindent                             " indent on cinwords

set shiftwidth=4                " set shiftwidth to 4 spaces

set tabstop=4                   " set tab to 4 spaces

set showmatch                   " Show matching brackets/braces/parantheses.

set background=dark     " set background to dark

set showcmd                             " Show (partial) command in status line.

set autowrite                   " Automatically save before commands like :next and :make

set textwidth=98                " My terminal is 98 characters wide

set visualbell                          " Silence the bell, use a flash instead

set cinoptions=:.5s,>1s,p0,t0,(0,g2     " :.5s = indent case statements 1/2 shiftwidth

set cinwords=if,else,while,do,for,switch,case,class,try   " Which keywords should indent

set showmatch

set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [ASCII=\%03.3b]\ [HEX=\%02.2B]\ [POS=%04l,%04v]\ [%p%%]\ [LEN=%L] "Shows detailed status line with formatting

set laststatus=2 "This Makes the status bar visible

set mat=5

set tabstop=2 shiftwidth=2 expandtab

filetype on

filetype plugin on

filetype indent on

set modeline

set mouse=a

set nocompatible

 

" vimrc file for following the coding standards specified in PEP 7 & 8.

"

" To use this file, source it in your own personal .vimrc file (``source

" <filename>``) or, if you don't have a .vimrc file, you can just symlink to it

" (``ln -s <this file> ~/.vimrc``).  All options are protected by autocmds

" (read below for an explanation of the command) so blind sourcing of this file

" is safe and will not affect your settings for non-Python or non-C files.

"

"

" All setting are protected by 'au' ('autocmd') statements.  Only files ending

" in .py or .pyw will trigger the Python settings while files ending in *.c or

" *.h will trigger the C settings.  This makes the file "safe" in terms of only

" adjusting settings for Python and C files.

"

" Only basic settings needed to enforce the style guidelines are set.

" Some suggested options are listed but commented out at the end of this file.

 

" Number of spaces that a pre-existing tab is equal to.

" For the amount of space used for a new tab use shiftwidth.

au BufRead,BufNewFile *py,*pyw,*.c,*.h,*.pl,*.pm,*.php set tabstop=8

 

" What to use for an indent.

" This will affect Ctrl-T and 'autoindent'.

" Python and PHP: 4 spaces

" C and perl : tabs (pre-existing files) or 4 spaces (new files)

au BufRead,BufNewFile *.py,*pyw,*.php set shiftwidth=4

au BufRead,BufNewFile *.py,*.pyw,*.php set expandtab

 

fu Select_c_style()

    if search('^\t', 'n', 150)

        set shiftwidth=8

        set noexpandtab

    el 

        set shiftwidth=4

        set expandtab

    en

endf

au BufRead,BufNewFile *.c,*.h,*.pl,*.pm,*.php call Select_c_style()

au BufRead,BufNewFile Makefile* set noexpandtab

 

" Use the below highlight group when displaying bad whitespace is desired.

highlight BadWhitespace ctermbg=red guibg=red

 

" Display tabs at the beginning of a line in Python mode as bad.

au BufRead,BufNewFile *.py,*.pyw match BadWhitespace /^\t\+/

" Make trailing whitespace be flagged as bad.

au BufRead,BufNewFile *.py,*.pyw,*.c,*.h,*.pl,*.pm,*.php match BadWhitespace /\s\+$/

 

" Wrap text after a certain number of characters

" Python: 79 

" C: 79

" Perl: 79

" PHP: 79

au BufRead,BufNewFile *.py,*.pyw,*.c,*.h,*.pl,*.pm,*.php set textwidth=79

 

" Turn off settings in 'formatoptions' relating to comment formatting.

" - c : do not automatically insert the comment leader when wrapping based on

"    'textwidth'

" - o : do not insert the comment leader when using 'o' or 'O' from command mode

" - r : do not insert the comment leader when hitting <Enter> in insert mode

" Python and Perl: not needed

" C: prevents insertion of '*' at the beginning of every line in a comment

au BufRead,BufNewFile *.c,*.h set formatoptions-=c formatoptions-=o formatoptions-=r

 

" Use UNIX (\n) line endings.

" Only used for new files so as to not force existing files to change their

" line endings.

" Python: yes

" C: yes

" Perl: yes

au BufNewFile *.py,*.pyw,*.c,*.h,*.pm,*.php set fileformat=unix

 

 

" ----------------------------------------------------------------------------

" The following section contains suggested settings.  While in no way required

" to meet coding standards, they are helpful.

 

" Set the default file encoding to UTF-8: ``set encoding=utf-8``

 

" Puts a marker at the beginning of the file to differentiate between UTF and

" UCS encoding (WARNING: can trick shells into thinking a text file is actually

" a binary file when executing the text file): ``set bomb``

 

" For full syntax highlighting:

"``let python_highlight_all=1``

"``syntax on``

 

" Automatically indent based on file type: ``filetype indent on``

" Keep indentation level from previous line: ``set autoindent``

 

" Folding based on indentation: ``set foldmethod=indent``

 

" Show tabs and trailing spaces.

" Ctrl-K >> for »

" Ctrl-K .M for ·

" (use :dig for list of digraphs)

set list listchars=tab:»»,trail:·

 

" my perl includes pod

let perl_include_pod = 1

" syntax color complex things like @{${"foo"}}

let perl_extended_vars = 1

 

" Fold the code block when <F2> is pressed

set foldmethod=marker

nmap <F2> 0v/{<CR>%zf




'Programming > Perl' 카테고리의 다른 글

Chapter 4 & Chapter 5  (0) 2012.10.05
Posted by 박세범