Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scalar = Variable?

Status
Not open for further replies.

y2k1981

Programmer
Aug 2, 2002
773
IE
Can somebody explain to me, is a scalar the same as an ordinary variable, eg is $a a scalar?
 
Hi,

A scalar is any 'singular' variable, ie.

$scalar = "bob";

$scalarReferenceToa = \$a;

$scalarAnonymousArrayReference = ['a','b','c'];

If it starts with a '$', it's a scalar.

A 'plural' variable is of course an array or hash. ie.

@array = ('a','b','c');
%hash = ('key1'=>'val1', 'key2'=>'val2');

Do you have a specific problem regarding perl variables. Let me know if so, and I'll try to help

Steve





 
Hi Steve,

Thanks for your post, and the offer of help. I'm just a beginner yet. You've managed to clear up my confusion though. I got a book on perl recently, but I'm beginning to think it's not a very good book! In the book I came across the following code:
Code:
if (@mydata)
{
print "The array has elements \n"
}
The book says that the mydata array is evaluated as a scalar, but I don't understand how? From what I can see, all it's doing is checking that the array contains elements... am I correct?

Regards
Martin
 
Hi Martin,

Not sure how well I'm able to explain this (hopefully one of the gurus here will do a better job) - basically Perl evaluates operations in either a scalar or list context.

In the case of the array test you give, Perl is evaluating the array in a scalar context, which returns the number of elements in the array. If an array is evaluated in list context it returns the elements of the array itself. The behaviour of various functions & operations in each context is dependant on the operation, but is usually fairly intuitive. You can force scalar context using the "scalar" function, so the test could have been written as:

if (scalar(@mydata))
{
print "The array has elements ", scalar(@mydata), "\n"
}

List contect can usually be forced using parentheses (), ie.

my $entireFile = (<FILEHANDLE>); ## this will evaluate the filehandle in list context. Filehandles return the rest of the file when called in a list context.


I had trouble getting my head round this stuff initally, but once you get used to using the different contexts, you realise just how good Perl is at doing what you expect.

If you're not using the Camel Book (Programming Perl) or the Llama Book (Learning Perl), I'd suggest you get them...the Llama book is intended for complete beginners, the Camel book for competent coders moving to Perl, I think.

Anyway, I've probably confused you even more, so I'll leave it at that!

Cheers

Steve
 
Hi Steve,

Thanks for the detailed post. It's beginning to make sense, but I still don't understand how saying if(@mydata) evaluates the array as a scalar?

The book did mention something about lists, but I thought it was just another name the author was using for an array!!!!!! You've definitely cleared up alot of confusion for me, and I'm very greatful.

maybe the book isn't so bad after all, it's probably just a matter of trying to get my head around it! Glad to see I'm not the only one who had difficulties with this.

Again, many thanks.

Martin :)
 
Hi,

&quot;...I still don't understand how saying if(@mydata) evaluates the array as a scalar?
&quot;

Me neither! It's part of the Perl language that it will try to do what you expect. It just comes with time & practice. I'm sure if I was a better scripter I'd learn exactly how this all works, but I don't need to....as long as it works as I expect!

Good luck.....Perl is worth the effort, believe me.

Steve
 
In response to:
------------------------------------------
my $entireFile = (<FILEHANDLE>); ## this will evaluate the filehandle in list context. Filehandles return the rest of the file when called in a list context.
------------------------------------------

This isn't correct. In order to create a 'list context', in this example the parentheses go around the left operand(s):

my ($entireFile) = <FILEHANDLE>;

This would be using the 'readline operator' in list context. But still you are not getting the whole file, just the first line. If you were assigning to an array, then each line of the file would be copied into the array - with each line being an array element.

If you wanted to just get the 'next' line from the file, using the 'readline operator' in scalar context (as shown in the original post) will return a single line starting with the first one - returning the next consecutive line each time it is used on the filehandle.

Concurring with Steve, the defacto book for Perl is &quot;Programming Perl 3rd Edition&quot; and it was co-authored by the guy who actually created Perl. If you read it from the beginning it really does a good job of explaining all the little 'catches' that perl trips new comers with. It's the best book.

--jim
 
From the perldata man page:
Code:
...
The Boolean context is just a special kind of scalar context where no conversion to a string or a number is ever performed.
...
If you evaluate an array in scalar context, it returns the length of the array.
...
Of course, the expression after the '
Code:
if
' is evaluated in boolean context.
 
I have to add my two cents here. Programming Perl is an excellent reference book but it is definitely not a good place to start if you want to learn Perl.

Learning Perl is the way to go to begin learning the language. It starts with the assumption that you are already a fairly competent programmer and introduces how to use Perl to accomplish things. What it doesn't do is teach you beginning programming. Likewise, it is not a reference book. Don't just put it on the shelf and go looking in it for answers to specific problems. Dedicate a few hours and actually read the first 120 or so pages. Do that and you'll come away with an understanding of why things are done a certain way in Perl; take that uinderstandnig, add a good reference book, and you're on your way.
 
I don't know. If I'm ever looking for a function, operator, or special variable - I check the camel. He knows all, making him my favorite reference material.

--jim
 
Hi,

Urrk. Don't know what I was thinking earlier...of course this

my $entireFile = (<FILEHANDLE>);

won't give the entire file....I'm tired & overworked at the moment though!

I'd usually do

my $entireFile = join ('',(<FILEHANDLE>));


Ta ta

Steve :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top