Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: My questions: new to perl

by ikegami (Pope)
on Mar 13, 2011 at 06:29 UTC ( #892916=note: print w/replies, xml ) Need Help??


in reply to My questions: new to perl

Explain difference between my and local ?

To explain the differences, there would have to similarities.

my creates a lexically scoped variable.

local temporarily saves a package variable, (Upd: array element or hash element. ) The previous value of the variable will be restored when the current scope is exited.

What is the purpose of -w , strict , -T ?

See perlrun for -w and -T.

strict detects errors that aren't detected by default for backwards compatibility reasons.

How do we set environment variables in perl ?

Assign to %ENV. See perlvar.

How to turn on perl warnings? Why is this important ?

use warnings; detects lots of situations which are very likely to have resulted from errors.

What are scalar data and scalar variables ?

"Scalar data" is by no means a formal term. I've never heard it. It probably refers to values that can be assigned to a scalar.

Scalar variables are one of Perl's variable types. They hold one* the following: a string, a signed integer, an unsigned integer, a floating point number, a string (array of 8-bit characters), a string (array of 32/64-bit characters), a reference or a glob.

(Upd: See perldata. )

* — Sometimes more than one, such as both the integer 123 and the string "123". These are optimisations or very special cases called "dualvars".

How do we read command line arguments with perl ?

They are provided in @ARGV. See perlvar.

Why does perl donot have overload functions ?

If there isn't, it's because noone coded a way of doing it.

How we sort a hash by the hash key ?

By definition, hashes can't be sorted. Their keys or values can be sorted.

(Upd: Magic can be used to alter their implementation. See reply for some modules that do this. )

How chomp( ) function and chop differs ?

chomp vs chop.

Replies are listed 'Best First'.
Re^2: My questions: new to perl
by davido (Cardinal) on Mar 13, 2011 at 06:37 UTC

    Great list of thoughtful answers. I noticed the question "What are scalar data and scalar variables ?" didn't have a reference. perldata has a good description of scalar data, and variable types including scalars.


    Dave

      Thanks, added.
Re^2: My questions: new to perl
by Anonymous Monk on Mar 13, 2011 at 06:34 UTC
      Ah yes. I didn't think of those because they aren't technically hashes. They do take on the same interface as a hash, so thanks for the correction.
Re^2: My questions: new to perl
by JavaFan (Canon) on Mar 13, 2011 at 12:06 UTC
    local temporarily saves a package variable. The previous value of the variable will be restored when the current scoped is exited.
    That's an incomplete answer. local can localized elements of lexical arrays (and hashes) as well.
    my @foo; sub hello {say "@foo";} @foo = qw[Hello, world!]; hello; { local $foo[1] = "earth!"; hello; } __END__ Hello, world! Hello, earth!
    strict detects errors that aren't detected by default for backwards compatibility reasons.
    That's the first time I hear someone claim strict has anything to do with backwards compatibility reasons. If that were true, no code written in the past 15 years would have had the need for "no strict".
    use warnings; detects lots of situations which are very likely to have resulted from errors.
    But on other cases, it's just plain wrong - specially in cases where a linter would have been more appropriate. For instance, the code I posted above isn't warnings free (but what is warned about should be done by a linter, not by warnings, IMO).

      That's an incomplete answer. local can localized elements of lexical arrays (and hashes) as well.

      Thanks, added.

      That's the first time I hear someone claim strict has anything to do with backwards compatibility reasons. If that were true, no code written in the past 15 years would have had the need for "no strict"

      You didn't finish your thought. Are you saying you disagree with the conclusion? How so?

      By the way, strict is now on by default in Perl 5.12 (the language, not the interpreter).*

      But on other cases, it's just plain wrong [...] For instance, the code I posted above isn't warnings free

      Comma in qw() is most definitely likely to be an error. I for one have taken advantage of this warning a couple of times.

      * — To use Perl 5.12, add use 5.012;.

      Update: Added "*".

        Are you saying you disagree with the conclusion?
        Most certainly.

        strict has been around as long as perl5. Which is as long as we have namespaces, and Exporter. Exporter only works by doing things strict forbids - but which weren't even possible before. So, strict forbids something that has only been possible for as long as strict exists. Not really "for backwards compatible reasons".

        By the way, strict is now on by default in Perl 5.12
        Except that it's not.
        print $], "\n"; $foo = 3; print $foo, "\n"; __END__ 5.012001 3
        Perhaps you mean that use 5.012; implies use strict;?
        Comma in qw() is most definitely likely to be an error.
        They are seldomly an error in my code (for instance, in the code shown), and nor are #'s in my qw's.
        I for one have taken advantage of this warning a couple of times.
        That I will not deny. But since the presence of commas can be detected without running the program, I do not think checking for this at every run is required. This is something that belongs in a linter. Perlcritic is a linter, and works wonders for tons of people.
Re^2: My questions: new to perl
by Monk_perl on Mar 14, 2011 at 19:19 UTC
    Thanks for providing direct links!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://892916]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2019-10-21 07:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?