in reply to Ideal way to pass a file to a module

Why not go for all of these?

sub add_cr { my %source= @_; my $content; # I suppose eventually that's what you want if( $source{content} { # content is in $source{content} } elsif( my $content_ref= $source{ref}) { # content in $$content_ref } elsif( my $filename= $source{filename}) { # you get the idea?...

Pass a hash <type of source> => <data> to the function, and get the real data based on the type of source.

Replies are listed 'Best First'.
Re: Re: Ideal way to pass a file to a module
by simonm (Vicar) on Apr 12, 2004 at 19:51 UTC
    Why not go for all of this?

    I agree, but I'd suggest a different interface: use separate functions for the different cases, and implement them in terms of one another. That allows users to import and call just those functions that they need.

    sub add_cr_to_str { # ... implementation goes here } sub add_cr_to_str_ref { add_cr_to_str( ${$_[0]} ) } sub add_cr_to_fh { add_cr_to_str( do { local $/; <$_[0]> } ) } sub add_cr_to_fn { require FileHandle; add_cr_to_sh( FileHandle->new( @_ ) ) }

    Most of those are just convenience functions, but if you later change your mind about which implementations are "primary", it's pretty easy to shuffle things around while maintaining the same interface.

    sub add_cr_to_str { add_cr_to_str_ref( \($_[0]) ) } sub add_cr_to_str_ref { # ... implementation goes here }