Data Dumper模組使用小例

2021-08-25 03:53:12 字數 1967 閱讀 9861

最近用了一把data::dumper模組,感覺很好很強大,故寫了個小例子。

data::dumper模組主要用途是:給出乙個或多個變數,包括引用,以perl語法的方式返回這個變數的內容。

比方說,這裡有個很複雜的hash,資料結構很複雜,我想看看這個hash裡面的內容。除了常見的方式(直接

用print或者編歷keys然後列印), 我們也可以使用data::daumper->dump([\%hash])的形式。同時,

模組中定義了很多的配置引數,讓使用者可以調整列印格式。

簡單列舉幾個(具體參見perldoc),這些變數在模組

$data::dumper::indent

這個設定列印的縮排格式,可以設定成0,1,2和3。使用者可以自己嘗試下。

$data::dumper::terse

如果設定這個變數,則不列印變數的名字,只列印變數的內容。

$data::dumper::maxdepth

不超過這個變數的限制深度,才列印變數的內容。

下面寫個程式說明問題:

#!/usr/bin/perl

use strict;

use warnings;

use data:

:dumper;my%

people=(

'name'

=>

'ray'

,'age'

=> 24,

'***'

=>

'man'

,'food'

=>

['egg',]

,);# see data::dumper module to get the default vaule of the

# following module gobal variable. you can overwrite the

# default value to user defined one.

print

"show perl hash, with pre-defined variable name\n "

;print

"and without maxdepth\n";$

data

::dumper:

:terse = 0;

# default is 0

$data

::dumper:

:indent = 3;

# default is 2

$data

::dumper:

:maxdepth = 0;

# default is 0my$

variable_name

='*'

."my_info"

;print data:

:dumper-

>

dump([

\%people],

[$variable_name])

;print

"show perl hash, without pre-defined variable name\n "

;print

"and with maxdepth is 1\n";$

data

::dumper:

:terse = 1;

# default is 0

$data

::dumper:

:indent = 2;

# default is 2

$data

::dumper:

:maxdepth = 1;

# default is 0

$variable_name

='$'

."my_info"

;print data:

:dumper-

>

dump([

\%people],

[$variable_name])

小程式藍芽模組

開啟連線後為了避免出現衝突 一旦開啟連線則終止掃瞄附近藍芽裝置 終止讀取本機已配對裝置 7 連線成功後根據deiviceid獲取裝置的所有服務 this getservice deviceid getservice function deviceid 獲取藍芽裝置service值wx.getbled...

python json模組小技巧

python的json模組通常用於與序列化資料,如 def get user info user id res json str json.dumps res return json str 但是當要序列化的資料裡面包含中文字元時,會變成這樣 import json res print json.d...

Python 小模組學習 logging

先看乙個比較簡單的例子,讓我們對logging模組有個感性的認識 import logging logging basicconfig filename os path join os getcwd log.txt level logging debug logging debug this is ...