In Perl, how can I find out if my file is being used as a module or run as a script?

后端 未结 3 393
旧巷少年郎
旧巷少年郎 2020-12-10 05:04

Let\'s say I have a Perl file in which there are parts I need to run only when I\'m called as a script. I remember reading sometime back about including those parts in a mai

3条回答
  •  萌比男神i
    2020-12-10 05:52

    Better to not do this, and instead take a structured approach like MooseX::Runnable.

    Your class will look like:

    class Get::Me::Data with (MooseX::Runnable, MooseX::Getopt) {
    
        has 'dsn' => (
            is            => 'ro',
            isa           => 'Str',
            documentation => 'Database to connect to',
        );
    
        has 'database' => (
            is         => 'ro',
            traits     => ['NoGetopt'],
            lazy_build => 1,
        );
    
        method _build_database {
            Database->connect($self->dsn);
        }
    
        method get_data(Str $for_person){
            return $database->search({ person => $for_person });
        }
    
        method run(Str $for_person?) {
            if(!$defined $for_person){
                print "Type the person you are looking for: ";
                $for_person = <>;
                chomp $for_person;
            }
    
            my @data = $self->get_data($for_person);
    
            if(!@data){
                say "No data found for $for_person";
                return 1;
            }
    
            for my $data (@data){
                say $data->format;
            }
    
            return 0;
        }
    }
    

    Now you have a class that can be used inside your program easily:

    my $finder = Get::Me::Data->new( database => $dbh );
    $finder->get_data('jrockway');
    

    Inside an interactive script that is bigger than just the "run" method above:

    ...
    my $finder = Get::Me::Data->new( dsn => 'person_database' );
    $finder->run('jrockway') and die 'Failure'; # and because "0" is success
    say "All done with Get::Me::Data.";
    ...
    

    If you just want to do this standalone, you can say:

    $ mx-run Get::Me::Data --help
    Usage: mx-run ... [arguments]
        --dsn     Database to connect to
    
    $ mx-run Get::Me::Data --dsn person_database
    Type the person you are looking for: jrockway
    
    
    $ mx-run Get::Me::Data --dsn person_database jrockway
    
    

    Notice how little code you wrote, and how flexible the resulting class is. "main if !caller" is nice, but why bother when you can do better?

    (BTW, MX::Runnable has plugins; so you can easily increase the amount of debugging output you see, restart your app when the code changes, make the app persistent, run it in the profiler, etc.)

提交回复
热议问题