|
NAMEIO::Util - A selection of general-utility IO functionVERSION 1.5The latest versions changes are reported in the Changes file in this distribution.INSTALLATION
SYNOPSISuse IO::Util qw(capture slurp Tid Lid Uid load_mml); capture() # captures the selected filehandle $output_ref = capture { any_printing_code() } ; # now $$output_ref eq 'something' # captures FILEHANDLE $output_ref = capture { any_special_printing_code() } \*FILEHEANDLER ; # append the output to $captured capture { any_printing_code() } \*FILEHEANDLER , \$captured # now $captured eq 'something' # use another class to tie the handler use IO::Scalar ; $IO::Util::TIE_HANDLE_CLASS = 'IO::Scalar' slurp() $_ = '/path/to/file' ; $content_ref = slurp ; $content_ref = slurp '/path/to/file' ; $content_ref = slurp \*FILEHANDLE ; # append the file content to $content $_ = '/path/to/file' ; slurp \$content; slurp '/path/to/file', \$content ; slurp \*FILEHANDLE, \$content ; Tid(), Lid(), Uid() $temporarily_unique_id = Tid ; # 'Q9MU1N_NVRM' $locally_unique_id = Lid ; # '2MS_Q9MU1N_P5F6' $universally_unique_id = Uid ; # 'MGJFSBTK_2MS_Q9MU1N_PWES' A MML file (Minimal Markup Language) <opt> <!-- a multi line comment--> <parA> <optA>01</optA> <optA>02</optA> <optA>03</optA> </parA> <parB> <optA>04</optA> <optA>05</optA> <optA>06</optA> <optB> <key>any key</key> </optB> </parB> </opt> load_mml() $struct = load_mml 'path/to/mml_file' ; $struct = load_mml \ $mml_string ; $struct = load_mml \ *MMLFILE ; $struct = load_mml ..., %options ; # $struct = { # 'parA' => { # 'optA' => [ # '01', # '02', # '03' # ] # }, # 'parB' => { # 'optA' => [ # '04', # '05', # '06' # ], # 'optB' => { # 'key' => 'any key' # } # } # } DESCRIPTIONThis is a micro-weight module that exports a few functions of general utility in IO operations.CAPTURING OUTPUTcapture { code } [ arguments ]Use this function in order to capture the output that a code write to any FILEHANDLE (usually STDOUT) by using "print", "printf" and "syswrite" statements. The function works also with already tied handles, and older perl versions.This function expects a mandatory code reference (usually a code block) passed as the first argument and two optional arguments: handle_ref and scalar_ref. The function executes the referenced code and returns a reference to the captured output. If handle_ref is omitted the selected filehandle will be used by default (usually "STDOUT"). If you pass scalar_ref, the output will be appended to the referenced scalar. In this case the result of the function will be the same SCALAR reference passed as the argument. You can pass the optional arguments in mixed order. All the following statement work: $output_ref = capture {...} $output_ref = capture {...} \*FH capture {...} \*FH, \$output ; # capure FH and append to $output capture {...} \$output, \*FH ; # capure FH and append to $output capture {...} \$output ; # append to $output capture \&code, ... # a classical code ref works too Note: This function ties the FILEHANDLE to IO::Util::WriteHandle class and unties it after the execution of the code. If FILEHANDLE is already tied to any other class, it just temporary re-bless the tied object to IO::Util::Handle class, re-blessing it again to its original class after the execution of the code, thus preserving the original FILEHANDLE configuration. SLURPING FILESslurp [ arguments ]The "slurp" function expects a path to a file or an open handle_ref, and returns the reference to the whole file|handle_ref content. If no argument is passed it will use $_ as the argument.As an alternative you can pass also a SCALAR reference as an argument, so the content will be appended to the referenced scalar. In this case the result of the function will be the same SCALAR reference passed as the argument. Note: You can pass the optional arguments in mixed order. All the following statement work: $content_ref = slurp ; # open file in $_ $content_ref = slurp '/path/to/file'; slurp '/path/to/file', \$content ; slurp \$content, '/path/to/file' ; slurp \$content ; GENERATING UNIQUE IDsThe "Tid", "Lid" and "Uid" functions return an unique ID string useful to name temporary files, or use for other purposes.Tid ( [options] )This function returns a temporary ID valid for the current process only. Different temporarily-unique strings are granted to be unique for the current process only ($$)Lid ( [options] )This function returns a local ID valid for the local host only. Different locally-unique strings are granted to be unique when generated by the same local hostUid ( [options] )This function returns an universal ID. Different universally-unique strings are granted to be unique also when generated by different hosts. Use this function if you have more than one machine generating the IDs for the same context. This function includes the host IP number in the id algorithm.*id optionsThe above functions accept an optional hash of named arguments:
$ui = Tid # Q9MU1N_NVRM $ui = Lid # 2MS_Q9MU1N_P5F6 $ui = Uid # MGJFSBTK_2MS_Q9MU1N_PWES $ui = Uid separator=>'-' # MGJFSBTK-2DH-Q9MU6H-7Z1Y $ui = Tid chars=>'base62' # 1czScD_2h0v $ui = Lid chars=>'base62' # rq_1czScD_2jC1 $ui = Uid chars=>'base62' # jQaB98R_rq_1czScD_2rqA $ui = Lid chars=>[ 0..9, 'A'..'F'] # 9F4_41AF2B34_62E76 Minimal Markup Language (MML)A lot of programmers use (de facto) a subset of canonical XML which is characterized by:No Attributes No mixed Data and Element content No Processing Instructions (PI) No Document Type Declaration (DTD) No non-character entity-references No CDATA marked sections Support for only UTF-8 character encoding No optional features That subset has no official standard, so in this description we will generically refer to it as 'Minimal Markup Language' or MML. Please, note that MML is just an unofficial and generic way to name that minimal XML subset, avoiding any possible MXML, SML, MinML, /.+ML$/ specificity. MML advantagesIf you need just to store configuration parameters and construct any perl data structure, MLM is all what you need. Using it instead full featured XML gives you a few very interesting advantages:
About XML parsing and structure reductionThe "load_mml" function produces perl structures exactly like other CPAN modules (e.g. XML::Simple, XML::Smart) but use the opposite approach. That modules usually require a canonical XML parser to achieve a full XML tree, then prune all the unwanted branches. That means thousands of line of code loaded and executed, and a potentially big structure to reduce, which probably is a waste of resources when you have just to deal with simple MML.The "load_mml" uses just a few lines of recursive code, parsing MML with a simple RE. It builds up only the branches it needs, optionally ignoring all the unwanted nodes. That is exactly what you need for MML, but it is obviously completely inappropriate for full XML files (e.g. HTML) which use attributes and other features unsupported by MML. load_mml ( MML [, options] )This function parses the MML eventually using the options, and returns a perl structure reflecting the MML structure and any custom logic you may need (see "options"). It accepts one MML parameter that can be a reference to a SCALAR content, a path to a file or a reference to a filehandle.This function accepts also a few options which could be passed as plain name=>value pairs or as a HASH reference. options You can customize the process by setting a few option, which will allow you to gain full control over the process and the resulting structure (see also the t/05_load_mml.t test file for a few examples):
parse_mml (id, MML [, options])Used internally and eventually by any handler, in order to parse any MML chunk and return its branch structure. It requires the element id, the reference to the MML chunk, eventually accepting the options hash reference to use for the branch.Note: You can escape any character (specially < and >) by using the backslash '\'. XML comments can be added to the MML and will be ignored by the parser. SUPPORT and FEEDBACKIf you need support or if you want just to send me some feedback or request, please use this link: http://perl.4pro.net/?IO::Util.AUTHOR and COPYRIGHT© 2004-2005 by Domizio Demichelis.All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as perl itself. POD ERRORSHey! The above document had some coding errors, which are explained below:
Visit the GSP FreeBSD Man Page Interface. |