Re: storing all type of vars
by davorg (Chancellor) on Sep 11, 2001 at 14:11 UTC
|
Your problem has nothing to do with references. You get
the same behaviour if you store any scalar value. As
busunsl points out, it's because you always return an
array from get. Another solution would be to always
use get in a list context, like this:
($refarr) = get();
As an aside, I'm not really sure what you're trying to
do here. What can you do with put and get
that you can't just use variables for?
--
<http://www.dave.org.uk>
Perl Training in the UK <http://www.iterative-software.com> | [reply] [d/l] |
|
|
This code is (in some way) part of a Stack Module.
The idea is to store whatever you want.
I first start the Stack Module to store only hash vars.
All the needs grow with time :) and I thought to store whatever I need
using the same module.
Sure I could find a Stack module in CPAN (I suspect) but is not funny and I learn less.
Danke busunsl, fine answer.
Sorry for my english (I try to learn too :) ).
| [reply] |
|
|
my @stack;
push @stack, 'something';
push @stack, 'some thing else';
push @stack, ['something', 'a', 'bit', 'more', 'complex'];
my $val = pop @stack;
--
<http://www.dave.org.uk>
Perl Training in the UK <http://www.iterative-software.com> | [reply] [d/l] |
|
|
|
|
Re: storing all type of vars
by busunsl (Vicar) on Sep 11, 2001 at 13:56 UTC
|
It doesn't work because you g'get' returns a list. If you evaluate that in scalar context you get the number of elements.
Use wantarray to check for the context:
{ my @store;
sub put {
@store = @_;}
sub get {
return wantarray ? @store : $store[0]; }}
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
Nice catch!! For those confused by this, see Arrays are not lists. It helped clarify the difference between the two (and more importantly, why you should care) in my mind.
-Blake
| [reply] |
|
|
Only if you define "a list" as being something rather specific that doesn't match many, many uses of the term "list" in even just the standard *.pod documentation.
Some argue that you can't "evaluate a list in a scalar context" because the scalar context prevents the creation of the list in the first place (clearly they have a different, specific definition of the term "list" in mind when they say this). Some argue that there is no general rule about what you get when you evaluate a list in a scalar context: you could get the last element, a count of elements, the first element, or something else entirely. This fits a bit closer to my thinking on the matter.
I prefer to be very careful and say that evaluting "something that would return a list if used in a list context" in a scalar context may get you any number of things. One of the most common is the last element of the list.
I also feel that the term "list" is used to mean so many different things in so many different standard texts on Perl that saying anything definitive about "a list" without first carefully defining what "a list" means to you (at that moment) is likely to be an incorrect or at least overbroad statement.
-
tye
(but my friends call me "Tye")
| [reply] |
Re: storing all type of vars
by George_Sherston (Vicar) on Sep 11, 2001 at 15:09 UTC
|
My £0.02 is that davorg is right in asking why you would want to do this - a constructive and helpful question. On the face of it, it looks a bit as though you want to turn every perl data structure into an array - but obviously that's not really what you intend. I'd be very interested to konw what these routines are for, what you wish to accomplish. Then maybe some wise monks will be able to suggest a way to do that which solves the problem.
Update: Ah, that makes sense. Welcome to the monastery, eod.
§ George Sherston | [reply] |
Re: storing all type of vars
by broquaint (Abbot) on Sep 11, 2001 at 15:15 UTC
|
As davorg has pointed out the errors in your code, I shall attempt to provide a working solution for your problem -
{
my @store;
sub put {
@store = ref($_[0]) ? @{$_[0]} : $_[0];
}
sub get {
return wantarray ? @store : \@store;
}
}
HTH
broquaint | [reply] [d/l] |
|
|
use strict;
{
my @store;
sub put {
@store = ref($_[0]) ? @{$_[0]} : $_[0];
}
sub get {
return wantarray ? @store : \@store;
}
}
my $scalar = 100;
put($scalar);
$scalar = get;
print "$scalar\n";
This prints:
ARRAY(0x20026814)
which means you're returning a reference when you should
just be returning the value.
--
<http://www.dave.org.uk>
Perl Training in the UK <http://www.iterative-software.com> | [reply] [d/l] [select] |
|
|
Of course, you're totally right.
Mental note: think then code, but firstly sleep...
broquaint
| [reply] |