Modules that you use can execute code

Exactly; and the code they execute may not be immediately obvious.

#!/usr/bin/perl use strict; use warnings; use EndRunner qw( foo ); sub foo_one { print "Foo one called!\n"; } sub foo_two { print "Foo two called\n"; } sub foo_dies { die "Aieee!\n"; } our $foo_bad_example = "no problem here"; __END__ ## This would go in EndRunner.pm package EndRunner; use strict; use warnings; our @ISA = qw( Exporter ); require Exporter; my @packages_to_run; sub import { my( $package, @args ) = @_; my $caller_package = ( caller( 1 ) )[0]; for my $prefix ( @args ) { push @packages_to_run, [ $caller_package, $prefix ]; } return; } END { no strict 'refs'; for my $items ( @packages_to_run ) { my( $package, $prefix ) = @{ $items }; for my $sub ( grep /^${prefix}_/, keys %{ $package . "::" } ) { my $sub_ref = *{"${package}::$sub"}{CODE}; next unless defined $sub_ref; eval { $sub_ref->(); }; if( $@ ) { warn "EndRunner: problem calling '$sub' in $package: $@\n"; } } } } 1; __END__

Just imagine what chaos one could foment if you changed that to (say) rot13 the prefix before picking what to run, or generated the prefix based on the phase of the moon . . . :)

The cake is a lie.
The cake is a lie.
The cake is a lie.


In reply to Re^2: Scripts with only sub-routines? by Fletch
in thread Scripts with only sub-routines? by kehansen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.