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

Re: localtime array slice problem

by dchetlin (Friar)
on Jan 11, 2001 at 20:33 UTC ( #51144=note: print w/ replies, xml ) Need Help??


in reply to localtime array slice problem

The @ as a dereferencing operator (as you are using it) expects a reference, and thus puts its argument in scalar context. The return value of localtime in scalar context is a scalar (a string, to be precise). You want localtime to be called in list context.

What you're looking for is (localtime)[1,3]. This causes localtime to be called in list context, and then takes elements 1 and 3 from the resultant list. However, be extra careful with that syntax when using it with print -- perl -e'print (localtime)[1,3]' won't work because it's interpreted as calling print with the argument localtime. Try either enclosing the whole slice in an extra set of parens, or putting a `+' in front of the first paren.

The reason your last example works is that you're return a single value from your subroutine, namely a reference to an array. @{...} dereferences that array, and then the slice grabs the elements you want from it. You could fix your first subroutine example similarly to the localtime fix.

Finally, and I know this is just an example, be very careful about naming subroutines `m'. It can cause some painful ambiguity with the m operator.

-dlc


Comment on Re: localtime array slice problem
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (14)
As of 2015-07-02 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (44 votes), past polls