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

I've got a rather perplexing problem.

I have a complex data structure, say:
$configs{images}{attach}{file}="height=19 width=15 src='/images/attach +/file.gif' border=0"; $configs{images}{attach}{URL}="height=19 width=15 src='/images/attach/ +URL.gif' border=0"; $configs{images}{attach}{edit}="height=19 width=15 src='/images/attach +/edit.gif' border=0"; $configs{mime_map} = MY::PATH::_get_mime_map ();
and later on: (although you shouldn't need it to answer this question)
sub _get_mime_map { my $mime_map = { '' => ["application/octet-stream","Unknown binary form +at"], # BEGIN these are non-standard 'aw' => ["application/applix","Applix Word Document"], 'as' => ["application/applix","Applix Spreadsheet"], 'ag' => ["application/applix","Applix Graphic"], # END these are non-standard 'ai' => ["application/postscript","Postscript ai File" +], 'aif' => ["audio/x-aiff","Aif File"], 'asc' => ["text/plain","ASCII Text File"] return($mime_map); }
I want to be able to call $configs->{image}{attach}{$mime_type} and have the html for it presented.
OK, now the problem is, I do NOT want to have to add a $configs{image}{attach} for each mime_type (there are plenty more, as you can imagine).

How can I tell $configs that if a {image}{attach}{UNKNOWN} is called that it doesn't know about, to create one with the same text as the others, but with the image file (at the end of the text) being the same as the missing (UNKNOWN) name (with .gif added)?
Hopefully I am presenting this clearly.

This is all being done in modules, mind you, with $configs being a common object to all the modules.

Any help would be appreciated.

What does this little button do . .<Click>; "USER HAS SIGNED OFF FOR THE DAY"

Replies are listed 'Best First'.
Re: Method autovivification with variable values?
by runrig (Abbot) on Sep 28, 2001 at 20:47 UTC
    A tied hash might be the ticket, which could return some default in the FETCH sub (or the value with a key of 'default' or somesuch or you could pass in the default key or value during the tie) if the key doesn't exist in the underlying real hash. See perltie. (Sorry for the lack of example, you can Super Search around here for examples, though).
Re: Method autovivification with variable values?
by Brovnik (Hermit) on Sep 28, 2001 at 21:02 UTC
    Turn attach into a function that stores the data internally and can do the checking.
    { my %store = (); sub attach { my ($type, $value) = @_; if ($value) { $store{$type} = $value}; } else { my $default = "height=19 width=15 src='/images/attach/UNKNOWN.gi +f' border=0"; return $store{$type} if $store{$type}; $default =~ s/UNKNOWN/$type/; return $default; } } } # Now, call attach('file',"height=19 width=15 src='/images/attach/file.gif' border +=0"); attach('URL',"height=19 width=15 src='/images/attach/URL.gif' border=0 +"); # to initialise, and attach($mime_type); # to access
    If you want to objectise it, make attach a method of the {image} object.

    Or, hide the function part by using Tie::Hash and tweaking the FETCH function to provide a default.
    --
    Brovnik

      We ended up changing $configs{images}{attach} to be a directory.
      So it worked like this
      package MY::PATH::Config.pm ...... more code ...... $configs{images}{attach} = "/path/to/image/dir/"; and later, in another module my $img = "$configs{images}{attach}" . "$att->{mime_type}" . ".gif"; <img src=$img>
      Which works, and allows us to pass almost any image type.
      We even made a ".gif" file (a copy of our unknown.gif) to
      handle blank mime types.

      Thanks for all the help everyone.

      What does this little button do . .<Click>; "USER HAS SIGNED OFF FOR THE DAY"
Re: Method autovivification with variable values?
by miyagawa (Chaplain) on Sep 28, 2001 at 20:49 UTC
    Make it a sub?
    sub get_img_test { return sprintf "height=19 width=15 src='/images/attach/%s.gif bord +er=0", shift; } $img_url = get_img_test('URL'); $img_unknown = get_img_test('UNKNOWN');
    If you want it done with hash, use tie.

    --
    Tatsuhiko Miyagawa
    miyagawa@cpan.org

Re: Method autovivification with variable values?
by dragonchild (Archbishop) on Sep 28, 2001 at 20:39 UTC
    Ummm...
    unless (exists $configs{image}{attach}{$text_to_check}) { # Do stuff here with $text_to_check }
    Is that what you're looking for?

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      'Fraid not. That would have to be done at the interface level, which would end up requiring said tests to be written into a dozen modules. I'm hoping for something which can be written into the same module as the $configs is defined, and thus can be used w/o editing the interface layer.

      Thanks for the try!

      What does this little button do . .<Click>; "USER HAS SIGNED OFF FOR THE DAY"
        You can do a tie to $configs, so that whenever a FETCH is done, it checks for existence and auto-vivifies before returning the value.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.