PHP에는 변수의 내용을 쉽게 볼 수 있는 print_r 이란 함수가 있다. 변수형태와 상관없이 변수명만 인자로 넘겨주면 변수에 저장된 내용을 재귀적으로 표시해 줘서 디버깅시 특히 요긴하게 쓸 수 있다. ASP에서는 안타깝게도 유사한 함수가 없어서 비슷한 기능을 하는 함수를 만들어 봤다.
<%
Sub print_r(var)
Dim myPrint
Set myPrint = New clsPrint
myPrint.print var, 0
End Sub
Class clsPrint
Public Sub print(var, nIndent)
Dim nType, sTypeName
'Response.Write TypeName(var)
nType = VarType(var)
Select Case nType
Case 0:
Response.Write "(vbEmpty)"
Case 1:
Response.Write "(vbNull)"
Case 2:
Response.Write var & "(vbIngerger)"
Case 3:
Response.Write var & "(vbLong)"
Case 4,5:
Response.Write var & "(vbSingle)"
Case 6:
Response.Write var & "(vbCurrency)"
Case 7:
Response.Write var & "(vbDate)"
Case 8:
Response.Write var & "(vbString)"
Case 9:'vbObject
sTypeName = TypeName(var)
Select Case sTypeName
Case "ISessionObject": 'Session
print_session var, nIndent
Case "IRequest": 'Request
print_request var, nIndent
Case "IApplicationObject": 'Application
print_application var, nIndent
Case Else
Response.Write "Unimpleneted TypeName:" & sTypeName
End Select
Case 10:
Response.Write "(vbError)"
Case 11:
Response.Write var & "(vbBoolean)"
Case 12:
Response.Write "(vbDataObject)"
Case 13:
Response.Write "(vbDecimal)"
Case 14:
Response.Write "(vbByte)"
Case 8192,8204:
print_array var, nIndent + 1
Case Else:
Response.Write "VarType:" & nType
End Select
Response.Write vbCrLf
End Sub
Sub print_array(var, nIndent)
print_list "Array", var, nIndent
End Sub
Sub print_request(var, nIndent)
print_name "Request", nIndent
print_openbrace nIndent
print_dict "Request.Form", var.Form, nIndent + 1
print_dict "Request.QueryString", var.QueryString, nIndent + 1
print_dict "Request.Cookies", var.Cookies, nIndent + 1
print_dict "Request.ServerVariables", var.ServerVariables, nIndent + 1
print_closebrace nIndent
End Sub
Sub print_application(var, nIndent)
print_name "Application", nIndent
print_openbrace nIndent
print_dict "Application.StaticObjects", var.StaticObjects, nIndent + 1
print_dict "Application.Contents", var.Contents, nIndent + 1
print_closebrace nIndent
End Sub
Sub print_session(var, nIndent)
print_name "Session", nIndent
print_openbrace nIndent
print_dict "Session.StaticObjects", var.StaticObjects, nIndent + 1
print_dict "Session.Contents", var.Contents, nIndent + 1
print_closebrace nIndent
End Sub
Sub print_dict(sTypeName, var, nIndent)
Dim sKey
print_name sTypeName, nIndent
print_openbrace nIndent
For Each sKey In var
print_key sKey, nIndent + 1
print var(sKey), nIndent + 1
Next
print_closebrace nIndent
End Sub
Sub print_list(sTypeName, var, nIndent)
Dim vValue, nIndex
Response.Write sTypeName & vbCrLf
print_openbrace nIndent
nIndex = 0
For Each vValue In var
print_key nIndex, nIndent + 1
print vValue, nIndent + 1
nIndex = nIndex + 1
Next
print_closebrace nIndent
End Sub
Private Sub print_name(sName, nIndent)
print_indent nIndent
Response.Write sName & vbCrLf
End Sub
Private Sub print_key(sKey, nIndent)
print_indent nIndent
Response.Write "[" & sKey & "] => "
End Sub
Private Sub print_openbrace(nIndent)
print_indent nIndent
Response.Write "(" & vbCrLf
End Sub
Private Sub print_closebrace(nIndent)
print_indent nIndent
Response.Write ")" & vbCrLf
End Sub
Private Sub print_indent(nIndent)
Response.Write String(nIndent * 4, " ")
End Sub
End Class
a = "123"
b = Split("1 2 3 4 5", " ")
print_r a
print_r b
print_r Session
'print_r Application
'print_r Request
%>
위를 브라우져에서 실행해서 소스보기를 하면 다음의 결과를 얻을 수 있다.
123(vbString) Array ( [0] => 1(vbString) [1] => 2(vbString) [2] => 3(vbString) [3] => 4(vbString) [4] => 5(vbString) ) Session ( Session.StaticObjects ( ) Session.Contents ( ) )
배열도 지원하고, Application, Session, Request 객체의 내용도 볼 수 있다. 자체적으로 쓰는 객체는 “Unimpleneted TypeName”이란 결과가 나올텐데, 이는 소스에다 해당 객체를 출력하는 부분을 추가해 주면 된다.