perl 정규 표현식 문제입니다.

dionysos의 이미지

use File::Find;
my $dir = './source'; 
find(\&print_if_file, $dir);
 
sub print_if_file {
	if( -f $_ ) {
		$targetFileName = $File::Find::name;
		$targetFileName =~ s/\//\\/g;
		print "STR = $targetFileName\n";
		open( fileHandle, $targetFileName ) || die "cannot open $targetFileName \n";
		while( $sLine = <fileHandle> )
		{
			print $sLine;
		}
		close( fileHandle );
 
	}
}

노가다 작업이 많아서 perl 스크립트를 배우고 있는 중입니다.

실행시키면 해당 하위 폴더인 source에 있는 모든 파일을 리스팅하고

그 파일들을 열어서 어떤 값을 바꿔주는 스크립트를 작성하고 있습니다.

파일을 찾으면 print_if_file 함수를 호출하게 되고 파일 이름이 $File::Find::name에 담기게 됩니다.

그 파일을 열어서 한 라인식 읽어서 다른 값으로 대치 시키려고 하는데

아무래도 파일이 열리지 않습니다.

무엇인 문제인가요?

aero의 이미지

File::Find 모듈이 파일을 찾고 해당 작업을 지정할때
기본으로 해당 디렉토리에 들어가 있는 상태로 작업이 됩니다.
위 코드에서 $targetFileName 에 상대경로가 들어 있는데
파일은 해당 디렉토리에 들어간 상태에서 열려고 하니 파일을
못찾는 겁니다.
따라서 File::Find 모듈의 옵션을 사용하여

find(\&print_if_file, $dir);

부분을
find( { wanted => \&print_if_file, no_chdir => 1}, $dir);

이렇게 바꾸고 해보면 될겁니다.
no_chdir => 1 의 의미는 change directory 하지말라는 의미입니다.

no_chdir 옵션을 사용하지 않는다면 파일 열기전에 $targetFileName에서
마지막 /포함 앞부분을 날려버리고 열어도 될겁니다.

자세한 옵션 설명은 File::Find 모듈 문서를 참고하세요.

dionysos의 이미지

스크립트 언어 첨 배우는 지라 어려움이 많네요.

이게 다 노가다 작업이 원인 ㅠㅠ.

삽질 계속하고 있었는데 명확하게 풀렸네요.

-------------------------------------------------------

노력은 배반하지 않는다.

노력은 배반하지 않는다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.