in reply to Maintaining strict refs while Binding a set of variables to @_ or dying if they are not defined

japhy, your code is close, but will not run under use strict. I have modified your example, but again I don't see a way of getting this to work under use strict.

use strict; sub file_name { no strict 'vars'; no strict 'refs'; my @arg = qw(client_number MM DD S); for (@arg) { $$_ = defined($_[0]) ? shift : die "$_ not defined" } "I$client_number$MM$DD$S" } my $out = file_name 1,2,3,4; warn $out;

But of course it can be improved with davorg's wonderful embellishment:

{ local $" "I@_" }

This code reminds me of learning shorthand.

  • Comment on Re: Maintaining strict refs while Binding a set of variables to @_ or dying if they are not defined
  • Select or Download Code

Replies are listed 'Best First'.
Re: Re: Maintaining strict refs while Binding a set of variables to @_ or dying if they are not defined
by japhy (Canon) on Nov 29, 2000 at 21:40 UTC
    Using davorg's little quoting nice-ness with a bit of finessing, we get:
    sub someFunc { my $i; local $"; for (qw( this that otherfield )) { die "$_ is undef" unless defined $_[$i++]; } "@_"; }


    japhy -- Perl and Regex Hacker
Re: Re: Maintaining strict refs while Binding a set of variables to @_ or dying if they are not defined
by japhy (Canon) on Nov 29, 2000 at 21:37 UTC
    It'd be easier if you reply to my message, so I don't have to search for a response. ;)

    Anyway, this code works for me:
    #!/usr/bin/perl -w use strict; sub someFunc { my $args = [ { client_number => 1, MM => 2, DD => 3, S => 4, }, ]; for (keys %$args) { my $idx = $args->[0]{$_} - 1; if (defined $_[$idx]) { $args->{$_} = $_[$idx], next } die "$_ not defined"; } print "@$args\n"; } someFunc(1,2,3,4); # fine someFunc(1,2,3); # 'S' is not defined
    It works under strict.

    japhy -- Perl and Regex Hacker