Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

My questions: new to perl

by Monk_perl (Initiate)
on Mar 13, 2011 at 05:41 UTC ( [id://892903]=perlquestion: print w/replies, xml ) Need Help??

Monk_perl has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: My questions: new to perl
by roboticus (Chancellor) on Mar 13, 2011 at 06:00 UTC

    Monk_perl:

    Hi friends..i want get these questions solved as i am near exams and can't solve these on my own..for you guys its a quick task i know. so here are these basic questions..i may get these Q's in exams..i am at basic level

    Perhaps it would've been a good idea to study? I'm certain your instructor probably already answered these questions in class.

    Anyway, your questions are easily answered:

    1 Explain difference between my and local ? Read perldoc perlfaq7 and perldoc perlvar

    2 What is the purpose of -w , strict , -T ? Read perldoc perlrun and perldoc strict

    3 How do we set environment variables in perl ? Read perldoc perlvar

    Well, this gets tiring. For the remainder of your questions, you should read perldoc perltoc to get an overview of the documentation available, and then read the ones appropriate to your questions. You'll also want to search older questions in the monastery for the answers.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: My questions: new to perl
by ikegami (Patriarch) on Mar 13, 2011 at 06:29 UTC

    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.

      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.
        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.
      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 "*".

      Thanks for providing direct links!
Re: My questions: new to perl
by biohisham (Priest) on Mar 13, 2011 at 15:06 UTC
    i want get these questions solved as i am near exams and can't solve these on my own

    This is kind of passively lazy... I mean, what were you doing throughout the academic term ? and when you say you can't solve that on your own, why not at least give it a try or work on your confidence threshold...

    8 questions answered may save the day for you on the examination day but we will be harming you I believe if we just answer your questions without you showing an attempt to try answering these on your own when majority of these are so simple and straightforward questions ...


    Excellence is an Endeavor of Persistence. A Year-Old Monk :D .
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://892903]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-03-28 15:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found