Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

OK, let's have a guess at the kind of thing you're doing. It probably differs in the details.

You have a script that needs to process a large directory tree; perhaps to perform backups or some other automated process. Some directories contain large text log files, which should be compressed before backing up, and once they're backed up they're never going to be modified so this knowledge allows us to shortcut a lot of processing. Other directories contain just images; these need to be backed up by uploading them to Flickr. And so on.

The way I'd approach this might be to keep a set of modules like this:

{ package Backup::General; sub backup_file { my ($class, $file) = @_; ... } sub backup_dir { my ($class, $dir) = @_; opendir my $d, $dir; while (readdir $d) { next if /^\./ or -d; $class->backup_file($_); } } ... } { package Backup::ImageDir; use parent 'Backup::General'; sub backup_file { my ($class, $file) = @_; $class->upload_flickr($file) if $file =~ /\.jpeg$/i; $class->SUPER::backup_file($file) } sub upload_flickr { ... } ...; } { package Backup::LogDir; use parent 'Backup::General'; sub backup_dir {...} ... }

Then each directory being backed up could contain a file backup.ini which looked something like this:

backup_class = "Backup::ImageDir"

My backup script would walk through the directories and for each:

use Config::Tiny; use Module::Runtime qw(use_package); my $config = Config::Tiny->read("$dir/backup.ini"); my $class = $config->{_}{backup_class} || 'Backup::General'; use_package($class)->backup_dir($dir);

That way, each directory decides for itself what module will handle its backups. There's no central list of backup modules; no limit to the number of different modules to choose from; the modules are loaded on demand as required.

This seems to be the sort of thing you want; some sort of general behaviour which can be overridden locally based on factors determined at runtime.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re^2: Redefine package subroutines by tobyink
in thread Redefine package subroutines by Melly

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (3)
    As of 2018-12-11 02:25 GMT
    Find Nodes?
      Voting Booth?
      How many stories does it take before you've heard them all?

      Results (52 votes). Check out past polls.

      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!