Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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.


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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://51144]
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: (10)
As of 2015-11-29 12:47 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (750 votes), past polls