in reply to Variable auto creation

You could do this:

#!/bin/perl -w my( $_var, $_val)= split /\s*=\s*/, $ARGV[0]; die "can't use $_var as a variable name" if( $_var=~ /^_va[rl]$/); ${$_var}= $_val;

This is higly unsafe though, you cannot use strict (at least not strict 'refs') and you risk overwriting one of you existing variables.

Why not just use a hash:

#!/bin/perl -w use strict; my( $_var, $_val)= split /\s*=\s*/, $ARGV[0]; my %hash=( $var => $val);

If you call both snippets with foo=bar as argument The first one lets you use $foo and the second one $hash{foo}.

Replies are listed 'Best First'.
RE: Re: Variable auto creation
by darkomen (Acolyte) on Sep 28, 2000 at 17:17 UTC
    The second snippet works great, but im not so familiar with hashes, is there any function like a push() for hashes? The code only works with one command line argument, i would like to be able to have multiple and "push" then into the hash with a foreach statment. Is that posible?