Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

localtime array slice problem

by InfiniteSilence (Curate)
on Jan 11, 2001 at 20:25 UTC ( #51141=perlquestion: print w/replies, xml ) Need Help??
InfiniteSilence has asked for the wisdom of the Perl Monks concerning the following question:

The perldoc on localtime tells that its results are "returned by the time function to a 9-element array ", but my attempts to do the following in ActivePerl return no results:

perl -e "print @{localtime}[1,3];"
I thought perhaps you couldn't force list context out of the return value from a function:
perl -e "sub m {return (1,2,3);} print @{&m}[0,1];" #fails
perl -e "sub m {@q=(1,2,3); return \@q;} print @{&m}[0,1];" #succeeds
Pardon my ignorance on all this. I am testing out stuff I am learning from Effective Perl Programming (this book rocks) on array slices.

Celebrate Intellectual Diversity

Replies are listed 'Best First'.
Re: localtime array slice problem
by dchetlin (Friar) on Jan 11, 2001 at 20:33 UTC
    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.


Re: localtime array slice problem
by ChOas (Curate) on Jan 11, 2001 at 20:36 UTC
    #!/usr/bin/perl -w use strict; my @List=(localtime)[1,3]; print @List;
    result: 3311

    your code with 'use strict':
    Ambiguous use of @{localtime} resolved to @localtime at ./tmp line 6. Global symbol "@localtime" requires explicit package name at ./tmp lin +e 6. Execution of ./tmp aborted due to compilation errors.

    Start with using strict... build from there, it will tell you
    when you are REALLY messing up


    print "profeth still\n" if /bird|devil/;
      Good point about trying new things out inside of a script with warnings and use strict on. In this case, since I already knew that localtime is a function, I was able to use the following to get right past both -w and use strict:
      #!/usr/bin/perl -w use strict; print @{localtime()}[1,3]; 1;
      Running this as
      perl -c
      checks out okay, but running it tells me that I "Can't use string ("Thu Jan 11.... as an ARRAY ref while in strict subs..."

      Quick question: Why did -c pass where running the script generates the uncaught exception?-

      Celebrate Intellectual Diversity

        Can't use string %s as %s ref while "strict refs" in use is a runtime error. -c is only for catching compile-time errors.

        There are many, many errors that can occur when a script is running but don't prevent the script from compiling. Here's another example:

        $x = 0; $y = 7 / $x;
Re: localtime array slice problem
by I0 (Priest) on Jan 11, 2001 at 20:35 UTC
    print @{[localtime]}[1,3]; or print ((localtime)[1,3]);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://51141]
Approved by root
[karlgoethebier]: The early bird catches the worm
holli enters he monastery hall, freezing but happy to have built the most beautiful snowman in the history of mankind
[holli]: tbh, the thing looks more like quasimode but hey, the thought matters. and it has blue LEDs as eyes
[holli]: we had solid 8cm fresh snow overnight. that is quite unusual for the area

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2017-12-18 05:51 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (467 votes). Check out past polls.