The best way to extend a given function is generally to add args to the end of the arg list. This should not break old code but there is always the possibility that it will. So if you have lots of args you want to pass then passing an arg hash is a sound approach....BUT it looks like the new() function in Net::SMTP might cause you grief.

If you trace through what occurs when you call send('smtp',@args) in Mime::Lite you effectively end up calling send_by_smtp(@args) which ends up with this:

my $smtp = MIME::Lite::SMTP->new(@args)

Given that this just calls the Mime::Lite::SMTP package which is a subclass of Net::SMTP.....this is where that call FINALLY ends up in Net::SMTP:

sub new { my $self = shift; my $type = ref($self) || $self; my $host = shift if @_ % 2; my %arg = @_; my $hosts = defined $host ? [ $host ] : $NetConfig{smtp_hosts}; my $obj; my $h; foreach $h (@{$hosts}) { $obj = $type->SUPER::new(PeerAddr => ($host = $h), PeerPort => $arg{Port} || 'smtp(25)', LocalAddr => $arg{LocalAddr}, LocalPort => $arg{LocalPort}, Proto => 'tcp', Timeout => defined $arg{Timeout} ? $arg{Timeout} : 120 ) and last; }

It seems to me that there is an assumption there that you are going to break. First the first arg to new() for Net::SMTP needs to be ($server/$host) and worse the %2 means it only recognises the $host arg if an ODD number of args is passed to new..... It then goes on to assume that all following args are a hash presented as a flat list. Adding your single scalar (hash ref) will cause it to explode with an odd number of elements in hash assignment error. Also passing {Debug=>1} as suggested will cause Net::SMTP to try to connect to a HASH REF which just aint gonna work. Presumably you plan to override this function in Mime::Lite - looks like you will have to to me.

Personally a standard coding practice around here is NEVER to write a function like func($scalar, @ary) - we always do func($scalar, \@ary) so we can easily add $forgot_this, $forgot_that to the end of the arg list func( $scalar, \@ary, $forgot_this, $forgot_that ). As soon as you code a trailing array into a function call you are screwed if you need to extend it without causing major dramas (ie breaking just about everything that calls that func)......

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: Extending MIME::Lite by tachyon
in thread Extending MIME::Lite by demerphq

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.