⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch07_07.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 7.6. Storing Files Inside Your Program Text (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:36:11Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch07_01.htm"TITLE="7. File Access"><LINKREL="prev"HREF="ch07_06.htm"TITLE="7.5. Creating Temporary Files"><LINKREL="next"HREF="ch07_08.htm"TITLE="7.7. Writing a Filter"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch07_06.htm"TITLE="7.5. Creating Temporary Files"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 7.5. Creating Temporary Files"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch07_01.htm"TITLE="7. File Access"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch07_08.htm"TITLE="7.7. Writing a Filter"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 7.7. Writing a Filter"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch07-14198">7.6. Storing Files Inside Your Program Text</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-642">Problem<ACLASS="indexterm"NAME="ch07-idx-1000009615-0"></A></A></H3><PCLASS="para">You have data that you want to bundle with your program and treat as though it were in a file, but you don't want it to be in a different file.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1000006794">Solution</A></H3><PCLASS="para">Use the <CODECLASS="literal">__DATA__</CODE><ACLASS="indexterm"NAME="ch07-idx-1000009621-0"></A><ACLASS="indexterm"NAME="ch07-idx-1000009621-1"></A> or <CODECLASS="literal">__END__</CODE> tokens after your program code to mark the start of a data block, which can be read inside your program or module from the <CODECLASS="literal">DATA</CODE> filehandle.</P><PCLASS="para">Use <CODECLASS="literal">__DATA__</CODE> within a module:</P><PRECLASS="programlisting">while (&lt;DATA&gt;) {    # process the line}__DATA__# your data goes here</PRE><PCLASS="para">Similarly, use <CODECLASS="literal">__END__</CODE> within the main program file:</P><PRECLASS="programlisting">while (&lt;main::DATA&gt;) {    # process the line}__END__# your data goes here</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-680">Discussion</A></H3><PCLASS="para"><CODECLASS="literal">__DATA__</CODE> and <CODECLASS="literal">__END__</CODE> indicate the logical end of a module or script before the physical end of file is reached. Text after <CODECLASS="literal">__DATA__</CODE> or <CODECLASS="literal">__END__</CODE> can be read through the per-package DATA filehandle. For example, take the hypothetical module Primes. Text after <CODECLASS="literal">__DATA__</CODE> in <EMCLASS="emphasis">Primes.pm</EM> can be read from the <CODECLASS="literal">Primes::DATA</CODE> filehandle.</P><PCLASS="para"><CODECLASS="literal">__END__</CODE> behaves as a synonym for <CODECLASS="literal">__DATA__</CODE> in the main package. Text after <CODECLASS="literal">__END__</CODE> tokens in modules is inaccessible.</P><PCLASS="para">This lets you write self-contained programs that would ordinarily keep data kept in separate files. Often this is used for documentation. Sometimes it's configuration data or old test data that the program was originally developed with, left lying about in case it ever needs to be recreated.</P><PCLASS="para">Another trick is to use <CODECLASS="literal">DATA</CODE> to find out the current program's or module's size or last modification date. On most systems, the <CODECLASS="literal">$0</CODE> variable will contain the full pathname to your running script. On systems where <CODECLASS="literal">$0</CODE> is not correct, you could try the DATA filehandle instead. This can be used to pull in the size, modification date, etc. Put a special token <CODECLASS="literal">__DATA__</CODE> at the end of the file (and maybe a warning not to delete it), and the DATA filehandle will be to the script itself.</P><PRECLASS="programlisting">use POSIX qw(strftime);$raw_time = (stat(DATA))[9];$size     = -s DATA;$kilosize = int($size / 1024) . 'k';print &quot;&lt;P&gt;Script size is $kilosize\n&quot;;print strftime(&quot;&lt;P&gt;Last script update: %c (%Z)\n&quot;, localtime($raw_time));__DATA__DO NOT REMOVE THE PRECEDING LINE.<CODECLASS="literal"></CODE><ACLASS="indexterm"NAME="ch07-idx-1000009623-0"></A><ACLASS="indexterm"NAME="ch07-idx-1000009623-1"></A>Everything else in this file will be ignored.<ACLASS="indexterm"NAME="ch07-idx-1000009617-0"></A></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-716">See Also</A></H3><PCLASS="para">The "Scalar Value Constructors" section of <ICLASS="filename">perldata</I> (1), and the <ACLASS="olink"HREF="../prog/ch02_03.htm#PERL2-CH-2-SECT-3.2.7">"Other literal tokens"</A> section of <ACLASS="olink"HREF="../prog/ch02_01.htm">Chapter 2</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch07_06.htm"TITLE="7.5. Creating Temporary Files"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 7.5. Creating Temporary Files"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch07_08.htm"TITLE="7.7. Writing a Filter"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 7.7. Writing a Filter"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">7.5. Creating Temporary Files</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">7.7. Writing a Filter</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -