Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

My questions: new to perl

by Monk_perl
on Mar 13, 2011 at 05:41 UTC ( #892903=perlquestion: print w/replies, xml ) Need Help??
Monk_perl has asked for the wisdom of the Perl Monks concerning the following question:

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 1 Explain difference between my and local ? 2 What is the purpose of -w , strict , -T ? 3 How do we set environment variables in perl ? 4 How to turn on perl warnings? Why is this important ? 5 What are scalar data and scalar variables ? 6 How do we read command line arguments with perl ? 7 Why does perl donot have overload functions ? 8 How we sort a hash by the hash key ? 9 How chomp( ) function and chop differs ?

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.

      Where i can find these ..plz help me is it on perl.org ? Read perldoc perlfaq7 and perldoc perlvar Read perldoc perlrun and perldoc strict Read perldoc perlvar perldoc perltoc Thanks for saying: When your only tool is a hammer, all problems look like your thumb

        Monk_perl:

        You can find just about everything at http://perldoc.perl.org:

        • perlvar Predefined variables in perl
        • perlrun Running perl, command-line arguments, ...
        • perlfunc Functions found in perl (chop, chomp, etc.)
        Also, if you're running perl at the command line, you can type perldoc perlvar to read the perlvar document (which is what I was referring to in my first reply). You can read about perldoc with the command perldoc perldoc which will give you some handy shortcuts, such as the one I use most: perldoc -f functionname which will give you the documentation for the function you specify.

        Finally, of course, there's google. Giving it a search like perl difference between chop chomp will lead you directly to the answer to one of your questions. Never forget about google when you're in a hurry.

        ...roboticus

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

        Where i can find these

        You can't find them anywhere, obviously; even after being given direct links; you waited too long

Re: My questions: new to perl
by ikegami (Pope) 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 .
      When i started studying then things was looking easier but not before i studied perl for one whole day as i started from morning fearing and thinking of exams very near so just don't have time to study in detail ..i don't want to waste a year so i asked Q's here i have some 14 more Q's i wana get help for solving here. Then i will study and put them to mind. I know these are straightforward Qs but not for me beacuse i have to study perl for days before i can say like you.i was not busy but i am kind of Lazy i accept it so now i just want to save time by straightforwdly getting answers than searching and studying so much stuff. I am going to ask some more Q's for following up as prepartion part.

        Apparently the professors wasted their time with you. You should fail the exam, but instead of "wasting a year" you should quit your studies and find a job that's simple enough for you.

        I hate those people wasting everyones time at a university, just waiting to get a piece of paper ...

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://892903]
Approved by moritz
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2017-12-12 22:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (340 votes). Check out past polls.

    Notices?