From what I understand, scalars make up any singular piece of data - it's when Perl has one of something and often this will be a number or a string but not an array or hash or other complex data type. You can identify a scalar variable because it's prefixed by a $ sign so if you see a variable named:
"$something"you know that it contains a singular piece of data.
Unlike other programming languages (Java), Perl is not strongly typed and is intelligent enough to know that when you have typed a string in quotes, you want it to behave like a string and when you type a number, you want it to behave like a number.
This means there is no faffing around telling the interpreter what kind of variable you want when it can work it out for itself. All you need to do is tell it what you want the variable to be called and what you want in that variable. Simple
There are ways of giving the variable a number but wanting it to behave like a string and vice versa but these have to explicitly be done. The default is to treat whatever it is as what it looks like - which sounds very sensible to me but I think there could be some potential problems with this so I guess it needs to be kept in mind.
Declaring and assigning a scalar
There are two ways to declare and assign a scalar variable; you can either do it all in one go:
my $name = "Emma";
or declare and assign separately:
my $name; #This is declaring the scalar
$name = "Emma;" #This is assigning it
This has basically said that there is a scalar variable called "name" and the value of this scalar is the string "Emma". The quotes just denote the beginning and the end of the string and won't actually be printed out. If you want to assign a number to a variable, you don't need to put the quotes but more on that later.
If we leave 'my' out and we have use strict turned on (which you should have!!), the program will fall to bits when you try to run it and complain at you with something that looks like this:
Global symbol "$name" requires explicit package name at declaration.pl line 5.
Execution of declaration.pl aborted due to compilation errors.
Not the friendliest of error messages, but I have seen worse. This error message could also mean that you've made a typo when writing a variable name. This will mean having different spellings between the declaration and any usage of it, which will make the interpreter cry.
So, now we know how to declare a scalar, the next question is what exactly can we store in these scalars? Posts to follow on numbers and strings...
Interesting blog. It's good to see a newbie's viewpoint on perl. I'll point students learning perl to it. Keep up the good work!
ReplyDeleteThank you! Glad you like it
Delete"The default is to treat whatever it is as what it looks like - which sounds very sensible to me but I think there could be some potential problems with this so I guess it needs to be kept in mind."
ReplyDeletePerl doesn't so much treat the scalar as what it looks like, but rather as you use it. The difference between Perl and other languages, it is often said, is that Perl is more concerned with verbs than nouns. If you join two scalar with + it does so as numbers, if you join them with . it does so as strings.
This is also a major reason that warnings is so important; it will warn when it thinks you have used an incompatible operation, like adding two strings that do not look like numbers.
Happy Perling!
This is a good start. Please note that there are other data types in Perl that could be prefixed with a '$'. For instance references:
ReplyDeletemy $person = {
name => 'fred',
pet => 'dino',
};
This declares a reference to a hash (aka hashref).
Hm, that part also caught my eye, but the way Emma described it (purposefully?) can also support refs - you could say a hashref contains/is a singular piece of information: a single reference to a whatever-it-is-referencing. :)
DeleteThanks for helping me to understand basic concepts. Thanks for your informative article. Java Training in Chennai | Pega Training in Chennai
ReplyDeleteThanks for the great blog! keep posting.
ReplyDeleteBest JAVA Training institute in Chennai
Java Training
Java Training in chennai
core Java training in chennai
Selenium Course in Chennai
Hadoop Training in Chennai
Best Selenium Training in chennai
Big Data Training in Chennai
Hey Nice Blog!! Thanks For Sharing!!!Wonderful blog & good post.Its really helpful for me, waiting for a more new post. Keep Blogging!
ReplyDeleteSEO company in coimbatore
SEO Service in Coimbatore
web design company in coimbatore
The site was so nice, I found out about a lot of great things. I like the way you make your blog posts. Keep up the good work and may you gain success in the long run.
ReplyDeleteBig Data Hadoop Training In Chennai | Big Data Hadoop Training In anna nagar | Big Data Hadoop Training In omr | Big Data Hadoop Training In porur | Big Data Hadoop Training In tambaram | Big Data Hadoop Training In velachery
It is really a nice and useful piece of info. I'm glad that you just shared this helpful information with us. Please stay us informed like this. Thanks for sharing.
ReplyDeleteamazon web services aws training in chennai
microsoft azure training in chennai
workday training in chennai
android-training-in chennai
ios training in chennai
no deposit bonus forex 2021 - takipçi satın al - takipçi satın al - takipçi satın al - takipcialdim.com/tiktok-takipci-satin-al/ - instagram beğeni satın al - instagram beğeni satın al - google haritalara yer ekleme - btcturk - tiktok izlenme satın al - sms onay - youtube izlenme satın al - google haritalara yer ekleme - no deposit bonus forex 2021 - tiktok jeton hilesi - tiktok beğeni satın al - binance - takipçi satın al - uc satın al - finanspedia.com - sms onay - sms onay - tiktok takipçi satın al - tiktok beğeni satın al - twitter takipçi satın al - trend topic satın al - youtube abone satın al - instagram beğeni satın al - tiktok beğeni satın al - twitter takipçi satın al - trend topic satın al - youtube abone satın al - instagram beğeni satın al - tiktok takipçi satın al - tiktok beğeni satın al - twitter takipçi satın al - trend topic satın al - youtube abone satın al - instagram beğeni satın al - perde modelleri - instagram takipçi satın al - instagram takipçi satın al - cami avizesi - marsbahis
ReplyDeleteucuz takipçi
ReplyDeleteucuz takipçi
tiktok izlenme satın al
binance güvenilir mi
okex güvenilir mi
paribu güvenilir mi
bitexen güvenilir mi
coinbase güvenilir mi
perde modelleri
ReplyDeletesms onay
VODAFONE MOBİL ÖDEME BOZDURMA
nft nasıl alınır
ANKARA EVDEN EVE NAKLİYAT
TRAFİK SİGORTASI
Dedektör
web sitesi kurma
aşk kitapları
lisans satın al
ReplyDeleteen son çıkan perde modelleri
yurtdışı kargo
özel ambulans
minecraft premium
nft nasıl alınır
en son çıkan perde modelleri
uc satın al