Far too often I find myself having to check a bunch of input args for definedness. It's annoying, and this annoyance is almost definitely the reason for defined-or in Perl 5.10. However, I need a solution that works in older perls.

I came up with an idea that seems to work, but it works by messing about with @_. This makes me nervous. Anyone care to tell me why this code is dangerous?

I've got two routines to munge @_. One changes the source array, called fix_undef(), which seems like bad news to me. The other, called no_sfx_fix_undef() seems to be safe.

So, as far as my limited brain can tell, the second function is safe to use unless I plan on using @_ to modify the original routine's arguments. Any other gotchas I should know about?

use strict; use warnings; my @args = ( qw/fee fie foe fum/, undef, 1..3 ); print "No SFX\n", '-'x10, "\n"; Sub_With_NO_Side_Effects( @args ); print map { "'$_'\n" } @args; print "\nSFX\n", '-'x10, "\n"; Sub_With_Side_Effects( @args ); print map { "'$_'\n" } @args; sub Sub_With_NO_Side_Effects { &no_sfx_fix_undef; print "$_: '$_[$_]'\n" foreach 0..$#_; } sub Sub_With_Side_Effects { &fix_undef; print "$_: '$_[$_]'\n" foreach 0..$#_; } # This sub modifies the original calling function's variables. BAD NE +WS sub fix_undef { defined $_ or $_ = '' foreach @_; } # This seems to be ok... sub no_sfx_fix_undef { @_ = map { defined $_ ? $_ : '' } @_; } __END__ # OUTPUT No SFX ---------- 0: 'fee' 1: 'fie' 2: 'foe' 3: 'fum' 4: '' 5: '1' 6: '2' 7: '3' Use of uninitialized value in concatenation (.) or string at C:\temp\t +est.pl line 9. 'fee' 'fie' 'foe' 'fum' '' '1' '2' '3' SFX ---------- 0: 'fee' 1: 'fie' 2: 'foe' 3: 'fum' 4: '' 5: '1' 6: '2' 7: '3' 'fee' 'fie' 'foe' 'fum' '' '1' '2' '3'


TGI says moo


In reply to Messing with @_ by TGI

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.