Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
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 rifling through the Monastery: (6)
As of 2014-08-29 02:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls