A common Ruby idiom involves creating all the classes and methods necessary for a given program and for other programs that might want to use them as in a library call, then in its own little section write the code that makes use of those classes and methods, creating something that works equally well as either a library or a stand-alone script. Thus, for a program that needs nothing but what's in this script/library you just execute the file as a stand-alone program. For a program that also needs other stuff, you can call the script/library as a library, ignore the program logic for running it as its own program, and just make use of the classes and methods in the file.
This is, in Ruby, accomplished by wrapping your stand-alone program logic in a conditional like so:
if __FILE__ == $0
# do stuff
end
If my first paragraph didn't make any sense to the reader, I hope the example sets things straight. Anyway, I know there's a $0 in Perl that does the same thing as $0 in Ruby, but I'm not able to find an equivalent for __FILE__ anywhere. Does such a thing exist, as such? If not -- what would be the Perl equivalent idiom?
print substr("Just another Perl hacker", 0, -2); |
|
- apotheon
CopyWrite Chad Perrin |
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|