Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

conditional self-execution

by apotheon (Deacon)
on Jan 28, 2008 at 03:04 UTC ( [id://664610]=perlquestion: print w/replies, xml ) Need Help??

apotheon has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: conditional self-execution
by kyle (Abbot) on Jan 28, 2008 at 03:11 UTC

    What you're describing sounds to me like a modulino.

    Update: In a nutshell, the way it's done is something like this:

    package Foo; _run( @ARGV ) if ! caller; sub _run { # Do stuff as a program }

    It's using caller to tell whether it's being executed "at the top level". If you search for modulino on CPAN, there are a few examples to look at.

Re: conditional self-execution
by gam3 (Curate) on Jan 28, 2008 at 03:24 UTC
    It all seems to work:
    do "q.pl"; # prints module
    ./q.pl # prints script
    # q.pl if (__FILE__ eq $0) { print "script\n"; } else { print "module\n"; }
    -- gam3
    A picture is worth a thousand words, but takes 200K.
Re: conditional self-execution
by GrandFather (Saint) on Jan 28, 2008 at 03:12 UTC

    umm, __FILE__ (did you try it?).

    Update: the Special Literals section in perldata provides the documentation for it - not my first guess I must admit.


    Perl is environmentally friendly - it saves trees

      Nope, didn't try it -- because in Ruby, __FILE__ is a variable (or constant, or something to that effect, unless I'm grossly mistaken), and in Perl it cannot be a variable thanks to the fact there's no leading sigil. I guess maybe I should have tried it anyway, on the wild-ass guess that maybe it'd magically work somehow regardless of my apparently false assumptions.

      I'd been searching for anything and everything I could think to search for that might give such information, using Google, since it didn't occur to me that there'd be a whole class of variable-like things called "special literals" in Perl.

      Anyway, thanks for pointing that out.

      print substr("Just another Perl hacker", 0, -2);
      - apotheon
      CopyWrite Chad Perrin

        It's the C legacy ya know ;).


        Perl is environmentally friendly - it saves trees
        actually, __FILE__ is exactly the same thing in perl: a variable, a constant, or something else.
Re: conditional self-execution
by talexb (Chancellor) on Jan 28, 2008 at 16:36 UTC
      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.

    OK -- after reading and re-reading the first sentence of your post, I think I understand the concept.

    So what's the point? It just strikes me as a clever piece of code, a solution looking for a problem. It makes more sense to me to just write a module, which is really just a library of related subroutines and variables, and call it from a script, which is just the main-line.

    And I'm asking this in the nicest possible way -- I'm not trashing Ruby, just I'm really puzzled as to why one would write something like this.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

      I get the feeling that, the more you get used to the idea of having that option, the more you'll find things that could be done better if you aren't afraid to use that option when it's available.

      In my personal case, though, the primary use so far has been for unit tests.

      print substr("Just another Perl hacker", 0, -2);
      - apotheon
      CopyWrite Chad Perrin

      First, it's not common. I personally only use it for tinkering but I don't publish that code. A few people use it as a place to put their unit tests, though that's still unusual.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://664610]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-19 06:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found