Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Re: Learning Exercise

by FoncÚ (Scribe)
on Jul 18, 2002 at 19:45 UTC ( #183009=note: print w/replies, xml ) Need Help??


in reply to Re: Learning Exercise
in thread Learning Exercise

Hmm...that makes a whole lot of sense. I do, however, have some questions:

1. I understand how if (/l/) { entry_list(); } works, but can you explain the premise behind /l/? Is that just how you list things in perl? Also...I knew there were cases when you could leave the & off when calling a sub, but how does that work?

2. || is equilivent to or?

3. Kind of like #2, does | just allow you to specify more than one option in an 'or' fashion?

4. What's the deal with subroutine cls?!

5. my $tmp = (); just sets $tmp to undef, right?

Alright, I believe that's all I have for now. Forgive the inexperience, please!

Replies are listed 'Best First'.
Re: Re: Re: Learning Exercise
by jarich (Curate) on Jul 19, 2002 at 01:06 UTC
    • 1a. /l/ says if there is an 'l' in $_ return true. This means that if you type ' l ' ('l' with some spaces) it'll still match. It will also match on 'list', 'look' and 'isn't that a nice whale over there?'. Personally I'd have done:
      s/\s//; # remove all whitespace from $_ if($_ eq "l") { entry_list(); } elsif($_ eq "a") { addentry();} #etc
      $_ is equivalent to your $option.
    • 1b. &s can be left off subroutine calls if the call is unambiguously a subroutine call. In this case (calling the subroutine with a pair of parens) the parens make it unambiguous. Calling the subroutine without parens but with & means that the subroutine gets given the contents of @_ as it's argument list. This is discouraged because it often frightens people.

      So there are these ways to call a subroutine (ignoring object methods):

      subroutine; # doesn't pass strict. &subroutine; # passes @_ as argument list &subroutine(); # looks weird, passes arguments in () subroutine(); # generally preferred method
    • 2. || does mean "or" but they are not equivalent in associativity. Look at "perldoc perlop" for more information.
    • 3. | allows alternation in regular expressions (those things between the //s). So yes, it does allow you to specify more than one option in an or-like fashion.
    • 4. Looks like it probably clears the screen/terminal. Have you tried using it?
    • 5. Yep. I think debiandude had originally intended tmp to be an array. my $tmp; has essentially the same effect. Of you can use my $tmp = undef;.
    Hope it helps

    jarich

Re: Re: Re: Learning Exercise
by debiandude (Scribe) on Jul 19, 2002 at 13:16 UTC
    >>2. || is equilivent to or? Yes also, && is and, ne is !=, not is ! and so on...

    >>. Kind of like #2, does | just allow you to specify more than one option in an 'or' fashion? | is the regex is the aleternation operator. Its pretty cool. You should check it out, very powerful :-)

    >> 4. What's the deal with subroutine cls?! Well you were using system("clear") or something like that which is not really portable. Those prints are the ansi escape sequence for clearing the screen and putting the cursor on the upper left corner.

    >> 5. my $tmp = (); just sets $tmp to undef, right? Yes. I was orgianally going to do something with it, besides being just a scalar but I didn't have enough time. So I tured it back into a scalar and never changed the parens, they're unnecessary. As I said it was a quick hack.
      I think I understand now. I need to continue trying these things out so I'll really get them.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2023-03-30 23:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (74 votes). Check out past polls.

    Notices?