Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Better to not do this, and instead take a structured approach like <a href="http://search.cpan.org/perldoc?MooseX::Runnable" rel="nofollow noreferrer">MooseX::Runnable</a>.</p> <p>Your class will look like:</p> <pre><code>class Get::Me::Data with (MooseX::Runnable, MooseX::Getopt) { has 'dsn' =&gt; ( is =&gt; 'ro', isa =&gt; 'Str', documentation =&gt; 'Database to connect to', ); has 'database' =&gt; ( is =&gt; 'ro', traits =&gt; ['NoGetopt'], lazy_build =&gt; 1, ); method _build_database { Database-&gt;connect($self-&gt;dsn); } method get_data(Str $for_person){ return $database-&gt;search({ person =&gt; $for_person }); } method run(Str $for_person?) { if(!$defined $for_person){ print "Type the person you are looking for: "; $for_person = &lt;&gt;; chomp $for_person; } my @data = $self-&gt;get_data($for_person); if(!@data){ say "No data found for $for_person"; return 1; } for my $data (@data){ say $data-&gt;format; } return 0; } } </code></pre> <p>Now you have a class that can be used inside your program easily:</p> <pre><code>my $finder = Get::Me::Data-&gt;new( database =&gt; $dbh ); $finder-&gt;get_data('jrockway'); </code></pre> <p>Inside an interactive script that is bigger than just the "run" method above:</p> <pre><code>... my $finder = Get::Me::Data-&gt;new( dsn =&gt; 'person_database' ); $finder-&gt;run('jrockway') and die 'Failure'; # and because "0" is success say "All done with Get::Me::Data."; ... </code></pre> <p>If you just want to do this standalone, you can say:</p> <pre><code>$ 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 &lt;data&gt; $ mx-run Get::Me::Data --dsn person_database jrockway &lt;data&gt; </code></pre> <p>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?</p> <p>(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.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload