[comp.lang.perl] ".." inside a variable

okamoto@hpcc01.HP.COM (Jeff Okamoto) (05/04/91)

I'm trying to get the .. construct to work when it is passed in as a command
line argument.  What I am having trouble with is getting the .. to properly
expand.  I've tried a bunch of ways to force the result into an array context,
but I'm obviously doing something wrong.

I've tried a number of variations on the following, to no avail:

	$a = "'A0'..'A9'"; # Eventually will be shift(@ARGV);

	eval "\@b = (\$a)";

Can anyone offer any suggestions?
-- 
 \      oo	The New Number Who,
  \____|\mm	Jeff Okamoto
  //_//\ \_\	HP Corporate Computing & Services
 /K-9/  \/_/	okamoto@ranma.corp.hp.com
/___/_____\	
-----------	(415) 857-6236

tchrist@convex.COM (Tom Christiansen) (05/08/91)

From the keyboard of okamoto@hpcc01.HP.COM (Jeff Okamoto):
:I'm trying to get the .. construct to work when it is passed in as a command
:line argument.  What I am having trouble with is getting the .. to properly
:expand.  I've tried a bunch of ways to force the result into an array context,
:but I'm obviously doing something wrong.
:
:I've tried a number of variations on the following, to no avail:
:
:	$a = "'A0'..'A9'"; # Eventually will be shift(@ARGV);
:
:	eval "\@b = (\$a)";
:
:Can anyone offer any suggestions?

I'm not sure why you're going through the eval.  I would just use:

    @b = 'A0' .. 'A9';

and be done with it.  If for some reason you must use the eval
way, remove the 2nd backslash:

:	eval "\@b = ($a)";

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
		"So much mail, so little time." 

niklas@appli.se (Niklas Hallqvist) (05/10/91)

okamoto@hpcc01.HP.COM (Jeff Okamoto) writes:

>I'm trying to get the .. construct to work when it is passed in as a command
>line argument.  What I am having trouble with is getting the .. to properly
>expand.  I've tried a bunch of ways to force the result into an array context,
>but I'm obviously doing something wrong.

>I've tried a number of variations on the following, to no avail:

>	$a = "'A0'..'A9'"; # Eventually will be shift(@ARGV);

>	eval "\@b = (\$a)";

>Can anyone offer any suggestions?

What version of perl are you running?  On my 4.003 the following
perl-script works allright:

#!/local/bin/perl
$range = shift;
eval "@array = $range";
print "@array\n";

When invoked as:
foo "'A1'..'A9'"

it prints:
A1 A2 A3 A4 A5 A6 A7 A8 A9

That's what you wanted, wasn't it?

						Niklas

lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) (05/10/91)

In article <1362@appli.se> niklas@appli.se (Niklas Hallqvist) writes:
: okamoto@hpcc01.HP.COM (Jeff Okamoto) writes:
: 
: >I'm trying to get the .. construct to work when it is passed in as a command
: >line argument.  What I am having trouble with is getting the .. to properly
: >expand.  I've tried a bunch of ways to force the result into an array context,
: >but I'm obviously doing something wrong.
: 
: >I've tried a number of variations on the following, to no avail:
: 
: >	$a = "'A0'..'A9'"; # Eventually will be shift(@ARGV);
: 
: >	eval "\@b = (\$a)";
: 
: >Can anyone offer any suggestions?
: 
: What version of perl are you running?  On my 4.003 the following
: perl-script works allright:
: 
: #!/local/bin/perl
: $range = shift;
: eval "@array = $range";
: print "@array\n";
: 
: When invoked as:
: foo "'A1'..'A9'"
: 
: it prints:
: A1 A2 A3 A4 A5 A6 A7 A8 A9
: 
: That's what you wanted, wasn't it?

This just happens to work because @array inside double quotes is
grandfathered not to work unless @array is referenced outside of
double quotes, to protect ancient scripts in which @array was never
interpolated.  You can prove this to yourself by adding

	@array;
	
at the end of your script--it stops working.  It's better to backwhack
the @array reference in the eval:

	eval "\@array = ($range)";  die $@ if $@;

If you put the parens around $range like that, you can also invoke it as

	foo "A1..A9,A13"

And checking $@ after evaluating user supplied strings will save a lot
of aggravation in the long run.

Larry