Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://51144]
[stevieb]: I'm working on hiding the fact the software needs MetaCPAN::Client, as I often have a hell of a time installing it. I'm going to change it to let the user know if they do revdep that it needs to be installed, and remove that distribution ..
[stevieb]: ...from being a prereq

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2017-06-25 23:40 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (572 votes). Check out past polls.